0

サンプル コードを使用して学習しますが、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);
    }
  }
4

1 に答える 1

1

mRegId に次の行の値があることを確認 (デバッグ) できる場合:

nameValuePairs.add(new BasicNameValuePair("regid", mRegId));

次に、次の行を次のように変更します。

    httpost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));

明示的なエンコーディングを設定します。

于 2012-11-25T20:28:28.500 に答える