(Android Studio)将应用程序连接到Google Endpoints Module

时间:2021-11-21 02:04:41

I'm having trouble following the second step here.

我在第二步遇到麻烦。

I really don't understand how this sample does anything other than return a simple toast message. How does it utilize the API to display that message?

除了返回简单的toast消息之外,我真的不明白这个示例是如何做的。它如何利用API来显示该消息?

class EndpointsAsyncTask extends AsyncTask<Pair<Context, String>, Void, String> {
private static MyApi myApiService = null;
private Context context;

@Override
protected String doInBackground(Pair<Context, String>... params) {
    if(myApiService == null) {  // Only do this once
        MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(),
                new AndroidJsonFactory(), null)
            // options for running against local devappserver
            // - 10.0.2.2 is localhost's IP address in Android emulator
            // - turn off compression when running against local devappserver
            .setRootUrl("http://10.0.2.2:8080/_ah/api/")
            .setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
                @Override
                public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
                    abstractGoogleClientRequest.setDisableGZipContent(true);
                }
            });
            // end options for devappserver

        myApiService = builder.build();
    }

    context = params[0].first;
    String name = params[0].second;

    try {
        return myApiService.sayHi(name).execute().getData();
    } catch (IOException e) {
        return e.getMessage();
    }
}

@Override
protected void onPostExecute(String result) {
    Toast.makeText(context, result, Toast.LENGTH_LONG).show();
}

I'm afraid my this sample is too complex for my limited knowledge. How exactly do I "talk" to the Google Endpoints Module when running an app? Specifically, What is EndpointsAsyncTask();?

我担心这个样本对我有限的知识太复杂了。运行应用程序时,我如何与Google端点模块“交谈”?具体来说,什么是EndpointsAsyncTask();?

Are there any resources listing all the methods available to me? Is there a simpler example of an app communicating with a Google Cloud Endpoint?

是否有任何资源列出了我可用的所有方法?是否有更简单的应用与Google Cloud Endpoint通信的示例?

1 个解决方案

#1


0  

The service methods available to you are defined by the backend source in section 1.

您可以使用的服务方法由第1节中的后端源定义。

In the example you posted, this line: myApiService.sayHi(name).execute() is an actual invocation call to the backend that you defined by annotating @ApiMethod("sayHi") on the method in the MyEndpoint.java class of your backend module.

在您发布的示例中,此行:myApiService.sayHi(name).execute()是对后端的实际调用调用,您通过在MyEndpoint.java类中的方法上注释@ApiMethod(“sayHi”)来定义后端模块。

The reason your Android app defines an EndpointsAsyncTask is because slow operations such as calls that hit the network need to happen off of the UI thread to avoid locking the UI. The demo simply puts the returned value into a Toast but you could modify onPostExecute() to do whatever you'd like with the result.

您的Android应用程序定义EndpointsAsyncTask的原因是因为访问网络的调用等慢速操作需要在UI线程之外发生,以避免锁定UI。该演示只是将返回的值放入Toast中,但您可以修改onPostExecute()以根据结果执行任何操作。

For more info on Google Endpoints check out: https://cloud.google.com/appengine/docs/java/endpoints/

有关Google终端的详细信息,请访问:https://cloud.google.com/appengine/docs/java/endpoints/

And for info about using an Android AsyncTask look here: http://developer.android.com/reference/android/os/AsyncTask.html

有关使用Android AsyncTask的信息,请访问:http://developer.android.com/reference/android/os/AsyncTask.html

#1


0  

The service methods available to you are defined by the backend source in section 1.

您可以使用的服务方法由第1节中的后端源定义。

In the example you posted, this line: myApiService.sayHi(name).execute() is an actual invocation call to the backend that you defined by annotating @ApiMethod("sayHi") on the method in the MyEndpoint.java class of your backend module.

在您发布的示例中,此行:myApiService.sayHi(name).execute()是对后端的实际调用调用,您通过在MyEndpoint.java类中的方法上注释@ApiMethod(“sayHi”)来定义后端模块。

The reason your Android app defines an EndpointsAsyncTask is because slow operations such as calls that hit the network need to happen off of the UI thread to avoid locking the UI. The demo simply puts the returned value into a Toast but you could modify onPostExecute() to do whatever you'd like with the result.

您的Android应用程序定义EndpointsAsyncTask的原因是因为访问网络的调用等慢速操作需要在UI线程之外发生,以避免锁定UI。该演示只是将返回的值放入Toast中,但您可以修改onPostExecute()以根据结果执行任何操作。

For more info on Google Endpoints check out: https://cloud.google.com/appengine/docs/java/endpoints/

有关Google终端的详细信息,请访问:https://cloud.google.com/appengine/docs/java/endpoints/

And for info about using an Android AsyncTask look here: http://developer.android.com/reference/android/os/AsyncTask.html

有关使用Android AsyncTask的信息,请访问:http://developer.android.com/reference/android/os/AsyncTask.html