0
我使用示例代码来学习,但RegID不发送到服务器。该调用是正确的,因为服务器接受它,没有错误,并在数据库中添加一行,但regid字符串为空。我在代码上研究了几个小时,但无法弄清楚什么是错误的。应用程序不会将GCM注册ID发送到服务器
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);
}
}
谢谢,检查和调整! – Harry
但是,当他向用户提供应用程序时,他无法确认其价值?我的意思是将用户注册ID发送到服务器。@ 323 –
这就是为什么您在发送给用户之前测试并运行您的代码。 – 323go