【发布时间】:2014-07-28 06:52:34
【问题描述】:
我正在使用下面页面上的示例代码尝试基于 GAE 的后端: https://github.com/GoogleCloudPlatform/gradle-appengine-templates/tree/master/GcmEndpoints
已经能够部署后端,我可以通过appspot.com链接-project-id.appspot.com在API explorer上查看和执行API
当我执行客户端应用程序(基于 Android)并调用
regService.register(regId).execute();
在服务器端,我在 Google Developer Console 上获得以下日志条目 -
"POST /registration/v1/registerDevice/APA91bHCCvjkMFdvf6YHh_rbdqdKMYoRnwm6iswQtTpztwCfNVWq_7xwSq1y9naiipYmfTrREInybypeLb5mc7LCzYGBSpC9jFM-Co_6xGUBiEjLyo1UT375ak7p0nrOiTdHFNwW7r31WYQJP7ojigRLxBTYvST4XTeNIufD6GHb3SbDFGl1hsc HTTP/1.1" 404 0 - "20773xxxxxxx Google-HTTP-Java-Client/1.17.0-rc (gzip)" "verlllll-auyyyy-zzz.appspot.com" ms=19 cpu_ms=0 app_engine_release=1.9.7 trace_id=ddfbcf4e13e27e2aa2a6c5e77bb8cc6f
在哪里:
- registration/v1/registerDevice/是后端服务的API/version/Method
-
APA91bHCCvjkMFdvf6YHh_rbdqdKMYoRnwm6iswQtTpztwCfNVWq_7xwSq1y9naiipYmfTrREInybypeLb5mc7LCzYGBSpC9jFM-Co_6xGUBiEjLyo1UT375ak7p0nrOiTdHFNwW7r31WYQJP7ojigRLxBTYvST4XTeNIufD6GHb3SbDFGl1hsc..是gcm.register(SENDER_ID)返回的设备注册id; gcm 属于 GoogleCloudMessaging 类型
20773xxxxxxx or SENDER_ID 是 Google Developer Console 上提供的项目编号。
& verllll-auyyyy-zzz.appspot.com 是项目 ID。
你能告诉我为什么我在响应中得到 HTTP/1.1 404 吗?
提前谢谢..
分享regService的搭建--
public class GcmRegistrationAsync extends AsyncTask<Context, Void, String> {
private Registration regService; // A Stub API from Server
private GoogleCloudMessaging gcm;
private Context context;
// TODO: change to your own sender ID to Google Developers Console project
// number, as per instructions above
private static final String SENDER_ID = "20773xxxxxxx";
public void GcmRegistrationAsyncTask(int i) {
Registration.Builder builder = new Registration.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), null)
// Need setRootUrl and setGoogleClientRequestInitializer only for local testing,
// otherwise they can be skipped
.setRootUrl("https://verlllll-auyyyy-zzz.appspot.com")
.setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
@Override
public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
abstractGoogleClientRequest.setDisableGZipContent(true);
}
});
i = this.test();
builder.setApplicationName(SENDER_ID);
regService = builder.build();
}
public int test () {
return 1;
}
@Override
protected String doInBackground(Context... params) {
int i = params.length;
context = (Context) params[0];
String msg = "test";
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context);
}
String regId = gcm.register(SENDER_ID);
msg = "Device registered, registration ID=" + regId;
// You should send the registration ID to your server over HTTP,
// so it can use GCM/HTTP or CCS to send messages to your app.
// The request to your server should be authenticated if your app
// is using accounts.
regService.register(regId).execute();
} catch (IOException ex) {
ex.printStackTrace();
msg = "Error: " + ex.getMessage();
}
return msg;
}
}
在 MyActivity.java 中
protected void onCreate(Bundle savedInstanceState) {
:
:
gcmRegistrationAsync = new GcmRegistrationAsync();
gcmRegistrationAsync.GcmRegistrationAsyncTask(1);
gcmRegistrationAsync.execute(this);
}
【问题讨论】:
-
你能分享你“构建” regService 的代码吗?