6

I've been following the tutorial in Creating an Endpoints Backend from an Android Project. Everything is being compiled and looks promising. The backend is up.

I changed CloudEndpointUtils#LOCAL_ANDROID_RUN to true. But when I try to test the app on a real device (not emulator) I'm failing on:

java.net.SocketException: failed to connect to /10.0.2.2 (port 8888) after 20000ms: isConnected failed: EHOSTUNREACH (No route to host)

So I looked at CloudEndpointUtils and saw that setting LOCAL_ANDROID_RUN to true makes the app look for DevAppServer in http://10.0.2.2:8888, which is the emulator virtual router.

But I'm not using the emulator. So I tried to change the constant holding this url to http://127.0.0.1:8888/ but still no luck. I'm now failing on:

java.net.ConnectException: failed to connect to /127.0.0.1 (port 8888) after 20000ms: isConnected failed: ECONNREFUSED (Connection refused)

I thought it might be because my PC is behind a firewall, so I disabled it, but still nothing.

Any suggestion would be appreciated.

Gryu
  • 2,102
  • 2
  • 16
  • 29
AsafK
  • 2,425
  • 3
  • 32
  • 38
  • since the device and server are on the same wifi network, i tried one more thing which is to use the server's ip on the network, and now im getting - java.net.SocketTimeoutException: failed to connect to /10.0.0.1 (port 8888) after 20000ms – AsafK Jul 31 '13 at 14:05
  • 1
    try changing host ip to 0.0.0.0 means it will accept from anywhere. Then point to your computer IP. – Faisal Jul 31 '13 at 14:46
  • i've added --address=*server_ip* argument to the App Engine server's Run configuration (in Eclipse). now when i open 'http://server_ip/8888' from the connected device's browser i finally see the "Cloud Endpoints Starter Template" page but... there is also en error 'Could not fetch URL: http://127.0.0.1:8888/_ah/spi/', which means somewhere the server still looks on localhost. i get the same error also when i call one of my endpoints from the app. – AsafK Jul 31 '13 at 15:32
  • @Faisal where should i change the host ip ? – AsafK Jul 31 '13 at 15:34
  • when starting the server probably that --address=0.0.0.0 so it's accessible from anywhere although the main problem seems to be that your generated library is pointing to localhost, probably change that too when debugging, I can't find where I read that I think it maybe a parameter. – Faisal Jul 31 '13 at 15:39
  • @Faisal please try to remember man. i've been banging my head with this for hours. – AsafK Jul 31 '13 at 16:38
  • add the --address="0.0.0.0" instead of server IP is what I meant on eclipse appengine server. 127.0.0.1 would fail to connect to itself when you specify a specific server. So 0.0.0.0 means accept from anywhere. I don't have any idea on what else if this don't work. – Faisal Jul 31 '13 at 17:14
  • it worked !!!! you are the king. – AsafK Jul 31 '13 at 17:56
  • cool! I added it as the answer. – Faisal Jul 31 '13 at 17:59

7 Answers7

12

If you want to do this in Android Studio, set httpAddress as follows in your build.gradle

appengine {

  ...

  httpAddress = "0.0.0.0"
}
kg.
  • 561
  • 6
  • 14
7

To accomplish this

  1. Put LOCAL_APP_ENGINE_SERVER_URL_FOR_ANDROID = your IP address (this is in your android App)
  2. Right click on App-Engine project -> Run-> Run Configurations -> Web Application(left bar) -> your app-engine project.

2.(Contniued) Go to Arugments -> add exactly like this

--address=0.0.0.0 --port=.......

*to get your ip address, go to cmd -> ipconfig -> add (IPV4 address with port number) in step 1

important note: Please change this everytime in case you are accessing internet from dongle or some dynamic IP internet service

Thankyou ... this works perfectly

user248187
  • 131
  • 1
  • 7
6

There are many answers here but I've found them messy or uncomplete. To run the local server on your computer and test it with a real device do this:

1) Add httpAddress = "0.0.0.0" to the appengine block in your build.gradle file of the backend (Google Cloud Endpoint) module, like this:

appengine {
    ...
    httpAddress = "0.0.0.0"
}

According to a comment on the question this means that 'will accept from anywhere'.

2) Run your backend module locally (ie. start the web server). This is trivial: select the backend configuration on the drop-down menu and press the green button. You can find more detailed instructions here.

You should now be able to open a browser (on your computer) and navigate to http://localhost:8080/ and see a webpage with 'Hello, Endpoints!' and many other stuff.

3) Find your computer's IP address (on a Mac go to System Preferences -> Network) and then set it as the root url in your Android app code, like this:

MyApi.Builder builder = new MyApi.Builder(
                            AndroidHttp.newCompatibleTransport(),
                            new AndroidJsonFactory(), 
                            null
).setRootUrl("http://192.168.1.36:8080/_ah/api/")

That's all!

Albert Vila Calvo
  • 15,298
  • 6
  • 62
  • 73
1

When locally testing Google cloud endpoints from none localhost address --address="0.0.0.0" setting might not be sufficient.

My Endpoints were giving 404 Not Found error when accessing from non localhost. I resolved it by including path parameter in API decoration.

My API annotation looked like this:

@ApiMethod(name="saveResponse", httpMethod = "post", path="saveResponse)
public Response saveResponse(@Named("answerString") String responseString)
virtas
  • 53
  • 8
0

Add the

--address="0.0.0.0" 

as the app engine server address parameter to accept from all incoming connections.

Faisal
  • 2,276
  • 15
  • 19
  • 3
    Can you please explain where to add this? What should be the value of `LOCAL_APP_ENGINE_SERVER_URL_FOR_ANDROID`? – user427969 Oct 20 '13 at 02:20
0

Follow the 2. point post by user248187 2.(Contniued) Go to Arugments -> add exactly like this --address=0.0.0.0 --port=.......

Now, On the client android code you need change the request IP (RootURL) of your services to local IP. You can do that by use builder.setRootUrl(). It is good that you add a flat for outomatice it on you android client, then it can switch between local and deployed app-engiene quickly. Such as:

//........................

private boolean mFlatLocalHostServer=true;  
private static final String LOCAL_IP = "192.168.1.11"; //Ip debug server
public static final String  LOCAL_SERVER_IP = "http://"+LOCAL_IP+":8888/_ah/api";
//......................
Mydataendpoint.Builder builder = new Mydataendpoint.Builder(
                                AndroidHttp.newCompatibleTransport(),
                                new GsonFactory(),
                                        mCredential); 

//request the  service to local ip 
if(mFlatLocalHostServer){
         builder.setRootUrl(LOCAL_SERVER_IP); 
}

Mydataendpoint service =  builder.build();
response=service.insertMyData(mydata).execute(); 
Campino
  • 682
  • 7
  • 11
-2

Address 10.0.2.2 is for accessing localhost using the emulator, to access localhost using a real device, use the following steps

-open your commandline (cmd) run ipconfig. -Get the ipaddress of your computer and replace 10.0.2.2 with it, -Build and run on your device.

Hope this helps someone

Ismael ozil
  • 573
  • 5
  • 6