我有一个Android Studio项目,其中包含Android应用程序和后端Google云端模块。 我遇到了开发后端的IP地址问题。 在Android文档的例子中,它说使用10.0.2.2 ,我试过了,我总是得到20秒的超时时间。 我也尝试使用我的开发机器的ip,这也会导致20秒的超时,即使它立即失败。 在之前的两种情况下,通过在浏览器中手动加载localhost:8080来验证服务器是否正在运行。
// Async task snippet protected Joke doInBackground(Void... voids) { if(mApiService == 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); } }); mApiService = builder.build(); } try { String text = mApiService.sayHi("Chris").execute().getData(); return new Joke(text); } catch (IOException e) { Log.d(TAG, "fetch: " + e.getLocalizedMessage()); return null; } }有任何想法吗?
I have an Android Studio project that contains the android app and a backend Google Cloud Module. I am having problems with the IP address of the development backend. In the examples in the Android docs it says to use 10.0.2.2, which I have tried and I always get a 20sec timeout. I have also tried to use my development machine's ip, which also results in a reported 20sec timeout, even though it fails immediately. In both of the previous cases the server is verified to be running by manually loading localhost:8080 in the browser.
// Async task snippet protected Joke doInBackground(Void... voids) { if(mApiService == 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); } }); mApiService = builder.build(); } try { String text = mApiService.sayHi("Chris").execute().getData(); return new Joke(text); } catch (IOException e) { Log.d(TAG, "fetch: " + e.getLocalizedMessage()); return null; } }Any ideas?
最满意答案
事实证明,我错过了关于在模拟器中运行的ip地址的文本片断,因为我正在usb连接的设备上测试它。 通过模拟器运行它工作得很好:)
It turns out that I missed the text snippet in regards to the ip address being for running within an emulator, where as I was testing it on a usb connected device. Running it via the emulator worked just fine :)
更多推荐
发布评论