I've got a raspberry pi running as a small server running Apache, PHP and a MySQL DB to hold the registrationIDs for GCM apps running on Android devices. When a device is registered it sends the regID to the DB.
The table is called registrations and it has columns device, project, owner, regid.
I should stress that the following code on the Android phone is all wrapped up in an AsyncTask and that it is invoked in the doInBackground method.
String script = "register_phone.php";
String serverUrl = SERVER_URL + script;
Map<String, String> params = new HashMap<String, String>();
params.put("regId", regId);
params.put("devId", devId);
params.put("projId", projId);
params.put("owner", owner);
// some other largely irrelevant stuff
post(serverUrl, params);
private static void post(String endpoint, Map<String, String> params)
throws IOException {
String url = null;
url = endpoint;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, String> param = iterator.next();
String key = param.getKey();
String value = param.getValue();
nameValuePairs.add(new BasicNameValuePair(key, value));
}
HttpResponse response;
final HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, NETWORK_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParams, NETWORK_TIMEOUT);
HttpConnectionParams.setStaleCheckingEnabled(httpParams, true);
HttpClient httpClient = new DefaultHttpClient(httpParams);
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
response = httpClient.execute(httpPost);
//int status = httpClient.getResponseCode();
Log.d(GCMTAG, "response " + response.getEntity());
String responseBody = "empty";
responseBody = EntityUtils.toString(response.getEntity());
Log.d(GCMTAG, "responsebody " + responseBody);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalArgumentException e){
e.printStackTrace();
}
}
}
On the server I have:
<?php
require 'vars.php';
$projid=$_POST['projId'];
$devid=$_POST['devId'];
$owner=$_POST['owner'];
$regid=$_POST['regId'];
//echo $owner; echo $projid; echo $devid;
$mysqli = new mysqli("localhost", $user, $pwd, $database);
$regidIN = mb_convert_encoding($regid, "UTF-8");
$projectIN = mb_convert_encoding($projid, "UTF-8");
$deviceIN = mb_convert_encoding($devid, "UTF-8");
$ownerIN = mb_convert_encoding($owner, "UTF-8");
$queryone="replace into registrations (device, project, owner, regid)
values ('$deviceIN', '$projectIN', '$ownerIN', '$regidIN')";
if (!($stmt = $mysqli->prepare( $queryone))) {
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
$stmt->close();
$mysqli->close();
?>
Where vars.php holds the passwords etc. These you might want to pass in from EdiTexts that the user completes in your application. It's up to you. Any way my vars.php looks like:
<?php
$user="hidden";
$pwd="secret";
$database="also_secret";
?>
I've left out a fair bit of extraneous stuff but you can see how data gets from the params on the Android side, to the POST variables in the PHP on the server and from there into the database. You should be able to adapt it for your needs, good luck.