如何使用 google 应用引擎从 android 设备访问本地主机?

How to access local host from android device using google app engine?

我正在使用 Android Studio 并尝试使用 google app engine 从 android 设备访问 local host。我遵循了这个 link.Now what they have shown it is nicely working with genymotion.Now when i tried with android device it didn't work.I read this post 并在我的运行中给出了这个配置,在 VM Args 中为

--地址=0.0.0.0
但是当我尝试运行后端服务器时,它给了我一些错误。

Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
Unrecognized option: --address=0.0.0.0

现在我有我的 SignUp class 与服务器通信。

public class SignUp_Endpoint_Communicator extends AsyncTask <Pair<Context, UserinfoModel>, Void, ResponseMessages> {
    private Context maincontext;
    private UserinfoModelApi userinfo_api;
    private UserinfoModel userdata;
    private manipulate_Signup ms;
    @Override
    protected ResponseMessages doInBackground(Pair<Context, UserinfoModel>... params) {
        if(userinfo_api == null) {  // Only do this once
            UserinfoModelApi.Builder builder = new UserinfoModelApi.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/")
                    .setRootUrl("http://0.0.0.0:8080/_ah/api/")
                    .setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
                        @Override
                        public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
                            abstractGoogleClientRequest.setDisableGZipContent(true);
                        }
                    });
            // end options for devappserver

            userinfo_api = builder.build();
        }
        maincontext = params[0].first;
        userdata = params[0].second;

        try {
            ResponseMessages response = new ResponseMessages();
            response = userinfo_api.setUserInfo(userdata).execute();
            return response;
        }
        catch (IOException e)
        {
            e.printStackTrace();
            return null;
        }
    }

    protected void onPostExecute(ResponseMessages response){
        ms = (manipulate_Signup) ((Activity) maincontext);
        ms.setResponseMessage(response);
    }

    public interface manipulate_Signup{
        public void setResponseMessage(ResponseMessages response);
    }
}

我是否需要更改任何其他内容才能在 android 设备上使用它?

你应该为本地主机使用 10.0.2.2.. 也许这个 link 可以帮助.. https://developer.android.com/tools/devices/emulator.html#networkaddresses

我处理了它。在android studio中,首先转到运行->编辑配置->后端->VM Args,然后粘贴我的本地数据存储箱所在位置的代码

-Ddatastore.backing_store=E:\datastore\local_db.bin

android 设备的 IP address 是您 wifiipaddress

我通过执行以下操作在 Android Studio 中实现了此功能:

  1. 创建 Builder 时,将 'rootUrl' 的默认“10.0.2.2”IP 地址替换为您的本地 IP 地址(我的是下面的 192.168.1.222)。

例如:

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
                    // - 192.168.1.222 is my machine's IP address on my local network.
                    // - turn off compression when running against local devappserver
                    .setRootUrl("http://192.168.1.222:8080/_ah/api/")
                    .setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
                        @Override
                        public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
                            abstractGoogleClientRequest.setDisableGZipContent(true);
                        }
                    });
  1. 编辑 Appengine 后端模块的 运行 配置:

    • 运行->编辑配置,select 您的 AppEngine 配置。
    • 取消选中 'Synchronize with build.gradle configuration'。
    • 将 'Server Address' 更改为 0.0.0.0
  2. 重新运行您的 Appengine 模块。

我四处走动,试图让它发挥作用,所以我希望它能为其他人节省一些时间。 Google 的相关文档似乎没有说明这一点。