【问题标题】:Google App Engine backend with Google Cloud Messaging带有 Google Cloud Messaging 的 Google App Engine 后端
【发布时间】: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

在哪里:

  1. registration/v1/registerDevice/是后端服务的API/version/Method
  2. 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 的代码吗?

标签: android google-app-engine


【解决方案1】:

要连接到端点,您必须连接到 https,如果您像以前一样(在 setUrl 中)连接到 http,则会收到 404 错误。

您的代码可能看起来更像:

Registration.Builder builder = new  Registration.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), null);

rootUrl 不需要设置,默认情况下它使用您的应用程序位置,当在本地运行时,将端点定向到您的 devapp 服务器(应用引擎本地测试服务器)很有用。

禁用压缩的代码也只是用来兼容devapp服务器

【讨论】:

  • 我尝试了同样的方法,但在错误消息中没有任何其他详细信息的情况下得到“404 Not Found”。使用 setUrl 中的 https 再次出现相同的详细错误..
  • 你仍然可以通过 api explorer 访问?
  • 是的,我可以通过 verlllll-auyyyy-zzz.appspot.com/_ah/api/explorer 访问并运行这些 API
  • 因此,如果 api explorer 工作正常,看起来您的问题肯定出在 JAVA 客户端。更改builder时错误是否有细微差别?
  • 是的,下面我只得到“404 Not Found” Registration.Builder builder = new Registration.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), null);
猜你喜欢
  • 1970-01-01
  • 2015-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多