I’ve scoured the internet for a while now and I cannot get my Android app to send the registration ID and device ID to the server. I’m sure there is something simple that I am missing, but can anyone see what I am doing wrong? The code is hit and the Toasts are shown, but the server doesn’t do anything, but if I go to the URL directly, it does what I want it to.
if (!regId.equals("")) {
Toast.makeText(context, "The registration ID is: " + regId,
Toast.LENGTH_LONG).show();
Toast.makeText(context, "The device ID is: " + deviceId,
Toast.LENGTH_LONG).show();
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://uni.britintel.co.uk/register.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("deviceId", deviceId));
nameValuePairs.add(new BasicNameValuePair("registrationId", regId));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse httpresponse = httpclient.execute(httppost);
BufferedReader rd = new BufferedReader(new InputStreamReader(
httpresponse.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
Log.e("HttpResponse", line);
Toast.makeText(context, line,
Toast.LENGTH_LONG).show();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
} else {
Toast.makeText(context, "The registration ID doesn't exist",
Toast.LENGTH_LONG).show();
}
The buffered reader will be removed once I get it working as it is pointless for what I want.
Thank you in advance for any help.
I have finally managed to get it to work.
I added the following permissions to the manifest xml, but not sure why it fixed it…
There were also some issues with my server, but I only got a response after adding the permissions.