无法从 Genymotion 模拟器连接到本地 gae 端点

Can't connect to local gae endpoints from Genymotion emulator

这是一个简单的 helloworld 项目,使用从 MyBeanMyEndpoint 类 生成的端点,这些端点默认为 Endpoints module。我使用的模拟器是 Genymotion,我通过 /10.0.2.2 ip 连接到它。当我在网络浏览器中本地 "localhost:8080" 访问端点服务器时它工作正常但是当我 run/debug 它在 AndroidStudio[= 中使用模拟器时39=] 我得到这个超时异常:

-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.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)

客户端模块: 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...
}

实际代码为运行: 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();

后端模块: 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;
    }

模块:应用程序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')
}

模块:后端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
    }
}

10.0.2.2 指的是您的 Genymotion 模拟器设备,它不是您的开发应用引擎服务器 运行。 您的开发应用程序引擎服务器应该在您的本地台式计算机上 运行,具有典型的 IP 地址,例如 192.168.1.100。所以改为这样做:

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

使用 cmd.exe window 中的 'ipconfig' 命令从 windows 中找出您的 IP 地址。我不知道其他 OS。请注意,您将在本地使用 'http',而不是 'https'(用于访问 appspot.com 上的应用程序引擎)

你所有的其他代码似乎都是正确的!