サンプル コードを使用して学習しますが、RegId がサーバーに送信されません。サーバーはそれを受け入れ、エラーはなく、データベースに行を追加しますが、regid 文字列は空であるため、呼び出しは正しいです。コードについて何時間も勉強しましたが、何が悪いのかわかりません。
主な活動
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);
}
}