无法连接到来自Genymotion仿真器的本地gae端点 [英] Can't connect to local gae endpoints from Genymotion emulator

查看:134
本文介绍了无法连接到来自Genymotion仿真器的本地gae端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的helloworld项目,它使用从 MyBean MyEndpoint 类生成的端点,这些类默认使用端点模块。我使用的模拟器是Genymotion,我通过 /10.0.2.2 ip连接到该模拟器。当我在我的Web浏览器中本地访问端点服务器localhost:8080时,它工作得很好,但是当我在 AndroidStudio 中使用模拟器运行/调试时,出现超时异常:

  -3571 / com.serjsmor.anotherbackend W / System.err:java.net.SocketTimeoutException:无法连接到/ 10.0.2.2(端口8080)20000ms后
03-03 11:44:30.310 3548-3571 / com.serjsmor.anotherbackend W / System.err:at libcore.io.IoBridge.connectErrno(IoBridge.java:169 )
03-03 11:44:30.310 3548-3571 / com.serjsmor.anotherbackend W / System.err:在libcore.io.IoBridge.connect(IoBridge.java:122)
03-03 11:44:30.310 3548-3571 / com.serjsmor.anotherbackend W / System.err:在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
03-03 11:44:30.310 3548-3571 /com.serjsmor.anotherbackend W / System.err:在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
03-03 11:44:30.310 3548-3571 / com.s erjsmor.anotherbackend W / System.err:在java.net.Socket.connect(Socket.java:882)
03-03 11:44:30.310 3548-3571 / com.serjsmor.anotherbackend W / System.err :at com.android.okhttp.internal.Platform.connectSocket(Platform.java:139)
03-03 11:44:30.310 3548-3571 / com.serjsmor.anotherbackend W / System.err:at com。 android.okhttp.Connection.connect(Connection.java:148)
03-03 11:44:30.310 3548-3571 / com.serjsmor.anotherbackend W / System.err:at com.android.okhttp.internal。 http.HttpEngine.connect(HttpEngine.java:276)
03-03 11:44:30.310 3548-3571 / com.serjsmor.anotherbackend W / System.err:at com.android.okhttp.internal.http。 HttpEngine.sendRequest(HttpEngine.java:211)
03-03 11:44:30.310 3548-3571 / com.serjsmor.anotherbackend W / System.err:at com.android.okhttp.internal.http.HttpURLConnectionImpl。执行(HttpURLConnectionImpl.java:373)
03-03 11:44:30.310 3548-3571 / com.serjsmor.anotherbackend W / System.err:at com.android.okh ttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
03-03 11:44:30.310 3548-3571 / com.serjsmor.anotherbackend W / System.err:at com.android.okhttp。 internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:208)
03-03 11:44:30.310 3548-3571 / com.serjsmor.anotherbackend W / System.err:at com.android.okhttp.internal。 http.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
03-03 11:44:30.310 3548-3571 / com.serjsmor.anotherbackend W / System.err:at com.android.okhttp.internal.http。 HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25)
03-03 11:44:30.310 3548-3571 / com.serjsmor.anotherbackend W / System.err:com.google.api.client.http.javanet。 NetHttpRequest.execute(NetHttpRequest.java:77)
03-03 11:44:30.310 3548-3571 / com.serjsmor.anotherbackend W / System.err:com.google.api.client.http.HttpRequest。执行(HttpRequest.java:965)
03-03 11:44:30.310 3548-3571 / com.serjsmor.anotherbackend W / System.err:位于com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410)
03-03 11:44:30.310 3548-3571 / com.serjsmor.anotherbackend W / System.err:位于com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
03-03 11:44:30.310 3548-3571 / com.serjsmor.anotherbackend W / System.err:com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
03-03 11:44:30.310 3548-3571 / com.serjsmor.anotherbackend W / System.err:at com.serjsmor.anotherbackend.MainActivity $ 1.run(MainActivity.java:25)
03-03 11: 44:30.310 3548-3571 / com.serjsmor.anotherbackend W / System.err:在java.lang.Thread.run(Thread.java:818)

客户端模块: TestEndpoint.java

  public class TestEndpoint {

final MyApi taskApiService;

//构造函数$ b $ public TestEndpoint(){
MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(),$ b $ new AndroidJsonFactory(),null )
.setRootUrl(https://10.0.2.2:8080/_ah/api/)
.setGoogleClientRequestInitializer(新的GoogleClientRequestInitializer(){
@Override
public void initialize (AbstractGoogleClientRequest<>> abstractGoogleClientRequest)
throws IOException {
abstractGoogleClientRequest.setDisableGZipContent(true);
}
}

);
taskApiService = builder.build();
} //结束构造函数,此类中的其他方法...
}

运行它的实际代码:
MainActivity.java

  new Thread(new Runnable() {
@Override
public void run(){
TestEndpoint test = new TestEndpoint();
try {
test.taskApiService.sayHi(sdad)。 ();
} catch(IOException e){
e.printStackTrace();
}
}
})。

后端模块
MyBean.java

  public class MyBean {

private String myData;

public String getData(){
return myData;
}

public void setData(String data){
myData = data;


$ / code>

MyEndpoint.java

  @Api(name =myApi,version =v1,namespace = @ApiNamespace(ownerDomain =backend.anotherbackend.serjsmor.com,ownerName = backend.anotherbackend.serjsmor.com,packagePath =))
public class MyEndpoint {
$ b $ / **
*一个简单的端点方法, Hi返回
* /
@ApiMethod(name =sayHi)
public MyBean sayHi(@Named(name)字符串名称){

MyBean响应= new MyBean();
response.setData(Hi,+ name);

返回响应;
}

模块:app build.gradle

  apply plugin:'com.android.application'

android {
compileSdkVersion 21
buildToolsVersion21.1.2

defaultConfig {
applicationIdcom.serjsmor.anotherbackend
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName1.0
$ b buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
}



依赖关系{
编译fileTree(dir:'libs',include:['* .jar'])
compile'c​​om。 android.support:appcompat-v7:21.0.3'
编译项目(路径:':后端',配置:'android-endpoints')
}

模块:后端build.gradle

 buildscript {
repositories {
jcenter()
}
依赖关系{
classpath'com.google.appengine:gradle-appengine -plugin:1.9.14'
}
}

知识库{
jcenter();
}

apply plugin:'java'
apply plugin:'war'
apply plugin:'appengine'

sourceCompatibility = JavaVersion .VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7

依赖项{
appengineSdk'com.google.appengine:appengine-java-sdk:1.9.14'
compile'c​​om .google.appengine:appengine-endpoints:1.9.14'
compile'c​​om.google.appengine:appengine-endpoints-deps:1.9.14'
compile'javax.servlet:servlet-api:2.5 '
}

appengine {
downloadSdk = true
appcfg {
oauth2 = true
}
端点{
getClientLibsOnBuild = true
getDiscoveryDocsOnBuild = true
}
}


解决方案

10.0.2.2是指您的Genymotion仿真器设备,它不是您开发应用程序引擎服务器运行的地方。
您的开发应用程序引擎服务器应该使用典型的IP地址(如192.168.1.100)在本地台式计算机上运行。因此,请改为:



.setRootUrl( http://192.168.1.100:8080/_ah/api/



使用'ipconfig'命令在cmd窗口找到你的ip地址.exe窗口。我不知道其他操作系统。注意你将在本地使用'http',而不是'https'(用于访问appspot.com上的应用引擎)

所有其他代码看起来都是正确的!

It's a simple helloworld project using endpoints generated from MyBean and MyEndpoint classes which come default with the Endpoints module. The emulator I use is Genymotion which I connect to through the /10.0.2.2 ip. When I access the endpoints server locally "localhost:8080" in my web browser it works just fine but when I run/debug it using emulator in AndroidStudio I get this timeout exception :

-3571/com.serjsmor.anotherbackend W/System.err﹕ java.net.SocketTimeoutException: failed to connect to /10.0.2.2 (port 8080) after 20000ms
    03-03 11:44:30.310    3548-3571/com.serjsmor.anotherbackend W/System.err﹕ at libcore.io.IoBridge.connectErrno(IoBridge.java:169)
    03-03 11:44:30.310    3548-3571/com.serjsmor.anotherbackend W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:122)
    03-03 11:44:30.310    3548-3571/com.serjsmor.anotherbackend W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
    03-03 11:44:30.310    3548-3571/com.serjsmor.anotherbackend W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
    03-03 11:44:30.310    3548-3571/com.serjsmor.anotherbackend W/System.err﹕ at java.net.Socket.connect(Socket.java:882)
    03-03 11:44:30.310    3548-3571/com.serjsmor.anotherbackend W/System.err﹕ at com.android.okhttp.internal.Platform.connectSocket(Platform.java:139)
    03-03 11:44:30.310    3548-3571/com.serjsmor.anotherbackend W/System.err﹕ at com.android.okhttp.Connection.connect(Connection.java:148)
    03-03 11:44:30.310    3548-3571/com.serjsmor.anotherbackend W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:276)
    03-03 11:44:30.310    3548-3571/com.serjsmor.anotherbackend W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
    03-03 11:44:30.310    3548-3571/com.serjsmor.anotherbackend W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:373)
    03-03 11:44:30.310    3548-3571/com.serjsmor.anotherbackend W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
    03-03 11:44:30.310    3548-3571/com.serjsmor.anotherbackend W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:208)
    03-03 11:44:30.310    3548-3571/com.serjsmor.anotherbackend W/System.err﹕ at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
    03-03 11:44:30.310    3548-3571/com.serjsmor.anotherbackend W/System.err﹕ at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25)
    03-03 11:44:30.310    3548-3571/com.serjsmor.anotherbackend W/System.err﹕ at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:77)
    03-03 11:44:30.310    3548-3571/com.serjsmor.anotherbackend W/System.err﹕ at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:965)
    03-03 11:44:30.310    3548-3571/com.serjsmor.anotherbackend W/System.err﹕ at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410)
    03-03 11:44:30.310    3548-3571/com.serjsmor.anotherbackend W/System.err﹕ at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
    03-03 11:44:30.310    3548-3571/com.serjsmor.anotherbackend W/System.err﹕ at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
    03-03 11:44:30.310    3548-3571/com.serjsmor.anotherbackend W/System.err﹕ at com.serjsmor.anotherbackend.MainActivity$1.run(MainActivity.java:25)
    03-03 11:44:30.310    3548-3571/com.serjsmor.anotherbackend W/System.err﹕ at java.lang.Thread.run(Thread.java:818)

client module : TestEndpoint.java

public class TestEndpoint {

    final MyApi taskApiService;

    // Constructor
    public TestEndpoint () {
        MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(),
                new AndroidJsonFactory(), null)
                .setRootUrl("https://10.0.2.2:8080/_ah/api/")
                .setGoogleClientRequestInitializer( new GoogleClientRequestInitializer() {
                    @Override
                    public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest)
                            throws IOException {
                        abstractGoogleClientRequest.setDisableGZipContent(true);
                    }
                }

                );
        taskApiService = builder.build();
    } // end of constructor, other methods to follow in this class...
}

The actual code to run it : MainActivity.java

new Thread( new Runnable() {
            @Override
            public void run() {
                TestEndpoint test = new TestEndpoint();
                try {
                    test.taskApiService.sayHi("sdad").execute();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

backend module : MyBean.java

public class MyBean {

    private String myData;

    public String getData() {
        return myData;
    }

    public void setData(String data) {
        myData = data;
    }
}

MyEndpoint.java

@Api(name = "myApi", version = "v1", namespace = @ApiNamespace(ownerDomain = "backend.anotherbackend.serjsmor.com", ownerName = "backend.anotherbackend.serjsmor.com", packagePath = ""))
public class MyEndpoint {

    /**
     * A simple endpoint method that takes a name and says Hi back
     */
    @ApiMethod(name = "sayHi")
    public MyBean sayHi(@Named("name") String name) {

        MyBean response = new MyBean();
        response.setData("Hi, " + name);

        return response;
    }

Module:app build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.serjsmor.anotherbackend"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile project(path: ':backend', configuration: 'android-endpoints')
}

Module:backend build.gradle

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.google.appengine:gradle-appengine-plugin:1.9.14'
    }
}

repositories {
    jcenter();
}

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'appengine'

sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7

dependencies {
    appengineSdk 'com.google.appengine:appengine-java-sdk:1.9.14'
    compile 'com.google.appengine:appengine-endpoints:1.9.14'
    compile 'com.google.appengine:appengine-endpoints-deps:1.9.14'
    compile 'javax.servlet:servlet-api:2.5'
}

appengine {
    downloadSdk = true
    appcfg {
        oauth2 = true
    }
    endpoints {
        getClientLibsOnBuild = true
        getDiscoveryDocsOnBuild = true
    }
}

解决方案

The 10.0.2.2 refers to your Genymotion emulator device which is NOT where your dev app engine server is running. Your dev app engine server should be running on your local desktop computer with a typical ip address like say 192.168.1.100. So do this instead:

.setRootUrl("http://192.168.1.100:8080/_ah/api/")

find out your ip address from windows with 'ipconfig' command in the cmd.exe window. I don't know for other OS. Notice you'll be using 'http' locally, NOT 'https' (which is for accessing the app engine on appspot.com)

All your other code seems RIGHT!

这篇关于无法连接到来自Genymotion仿真器的本地gae端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆