Reputation: 796
I use a sample code to learn but the RegId is not send to the server. The call is correct because the server accepts it, no error, and add a row in the database but the regid string is empty. I studied hours on the code but can not figure out what is wrong.
MainActivity
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (!regId.equals("")) {
sendIdToServer(regId);
} else {
GCMRegistrar.register(this, Constants.SENDER_ID);
}
...
private void sendIdToServer(String regId) {
String status = getString(R.string.gcm_registration, regId);
mStatus.setText(status);
(new SendRegistrationIdTask(regId)).execute();
}
...
private final class SendRegistrationIdTask extends
AsyncTask<String, Void, HttpResponse> {
private String mRegId;
public SendRegistrationIdTask(String regId) {
mRegId = regId;
}
@Override
protected HttpResponse doInBackground(String... regIds) {
String url = Constants.SERVER_URL;
HttpPost httppost = new HttpPost(url);
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("regid", mRegId));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpClient httpclient = new DefaultHttpClient();
return httpclient.execute(httppost);
} catch (ClientProtocolException e) {
Log.e(Constants.TAG, e.getMessage(), e);
} catch (IOException e) {
Log.e(Constants.TAG, e.getMessage(), e);
}
return null;
}
@Override
protected void onPostExecute(HttpResponse response) {
if (response == null) {
Log.e(Constants.TAG, "HttpResponse is null");
return;
}
StatusLine httpStatus = response.getStatusLine();
if (httpStatus.getStatusCode() != 200) {
Log.e(Constants.TAG, "Status: " + httpStatus.getStatusCode());
mStatus.setText(httpStatus.getReasonPhrase());
return;
}
String status = getString(R.string.server_registration, mRegId);
mStatus.setText(status);
}
}
Upvotes: 0
Views: 2719
Reputation: 14274
If you can confirm (debug) that mRegId has a value in this line:
nameValuePairs.add(new BasicNameValuePair("regid", mRegId));
then change the next line to:
httpost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
to set explicit encoding.
Upvotes: 1