使用Google Cloud Messaging的Google App Engine后端

时间:2014-07-28 06:52:34

标签: android google-app-engine

我正在使用以下页面上的示例代码尝试基于GAE的后端: https://github.com/GoogleCloudPlatform/gradle-appengine-templates/tree/master/GcmEndpoints

已经能够部署后端,我可以查看&通过appspot.com链接 - project-id.appspot.com

在API资源管理器上执行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 /版本/方法
  2. APA91bHCCvjkMFdvf6YHh_rbdqdKMYoRnwm6iswQtTpztwCfNVWq_7xwSq1y9naiipYmfTrREInybypeLb5mc7LCzYGBSpC9jFM-Co_6xGUBiEjLyo1UT375ak7p0nrOiTdHFNwW7r31WYQJP7ojigRLxBTYvST4XTeNIufD6GHb3SbDFGl1hsc ..是gcm.register返回的设备注册ID(SENDER_ID); gcm的类型为GoogleCloudMessaging
  3. 20773xxxxxxx or SENDER_ID是Google Developer Console上提供的项目编号。 &安培; verlllll-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);
    }
    

1 个答案:

答案 0 :(得分:2)

要连接到端点,您必须与https连接,如果您与http连接(在您的setUrl中),则会收到404错误。

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

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

rootUrl不需要设置,默认情况下它使用你的appspot位置,在本地运行时,将端点指向你的devapp服务器(app引擎本地测试服务器)很有用。

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