从Android客户端调用端点时连接被拒绝

时间:2016-06-25 18:34:52

标签: java android google-app-engine asynchronous google-cloud-endpoints

我的Android应用使用google端点作为可扩展的后端解决方案。但是,我无法从Android客户端调用它。

我从一个活动调用一个异步任务类,在异步任务类中调用endpoints方法。在这种情况下,将调用默认的sayHi()方法,并且应使用onPostExecute(String result)方法在toast中显示消息。

但是,当我在模拟器中运行app模块时,此错误会出现在toast中:

failed to connect to /10.0.2.2 (port 8080) after 20000ms: isConnected failed: ECONNREFUSED (Connection refused)

以下是从活动中调用异步任务的代码:

Profile profile = new Profile(firstName, lastName, birthday);
new EndpointTask(). new SaveProfileTask(profile, this).execute();

这是异步任务类:

public class EndpointTask {

    public class SaveProfileTask extends AsyncTask<Void, Void, String> {
        private final String LOG_TAG = SaveProfileTask.class.getSimpleName();
        private MyApi mApi;
        private Profile mProfile;
        private Context mContext;

        public SaveProfileTask(Profile profile, Context context) {
            mProfile = profile;
            mContext = context;
        }

        @Override
        protected String doInBackground(Void... params) {
            if (mApi == null) {
                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);
                            }
                        });
                mApi = builder.build();
            }

            try {
                return mApi.sayHi(mProfile.getFirstName()).execute().getData();
            } catch (IOException e) {
                return e.getMessage();
            }
        }

        @Override
        protected void onPostExecute(String result) {
            Toast.makeText(mContext, result, Toast.LENGTH_LONG).show();
            Log.d(LOG_TAG, "ERROR: " + result);
        }
    }
}

大部分内容来自github repo here。这是使用谷歌应用引擎的后端连接客户端的官方Android文档的一部分。

为什么会出现此错误以及如何解决?

谢谢大家,快乐的编码。

0 个答案:

没有答案