João Correia
João Correia

Reputation: 105

Android Httpget time out

I'm trying to get some data from a MySQL DB via php script. My code is : `

            HttpClient httpclient = new DefaultHttpClient();

            HttpGet httpget = new HttpGet("http://example.com/fscript.php");
            HttpResponse response= null;
            System.out.println(httpget.toString());
            try{
               response= httpclient.execute(httpget); 
            }catch(Exception e){
               e.printStackTrace();
            }

and I always get a timeout in the LogCat and in the StackTrace:

java.net.SocketException: The operation timed out

when I use the same code but pointing to http://10.0.2.2:90/fscript.php, a server I have in my development machine, it works fine, so I think is something related with the remote server. And when I write the address in a browser it returns the json string that my script returns, so I guess the problem isn't the php either. In my manifest I have the Internet permission in the application, do I need something else? Or maybe this is something I must configure in my webserver to accept calls from android?

Just guessing, will appreciate any help.

Upvotes: 0

Views: 857

Answers (3)

Eamorr
Eamorr

Reputation: 10012

I'm using language loosely here: 10.0.2.2 is Android's localhost which points to 127.0.0.1 on your machine. Therefore, if your script operates from http://127.0.0.1:90/fscript.php, then it should be visible at http://10.0.2.2:90/fscript.php on Android.

Upvotes: 0

Marc B
Marc B

Reputation: 360762

Your webserver couldn't care less that the call is from an Android device. It just sees an HTTP request come in and deals with it accordingly. I'd investigate if your Android device is actually able to reach out to a non-local address in the first place. e.g. Maybe a firewall is blocking the request. If this is in the Android emulator, make sure that its network bridging is properly configured.

Upvotes: 0

Pratik
Pratik

Reputation: 30855

possible duplicate answer

This is happening because some times your server is taking too long to respond. Actually this could also happening due to a slow network, so you don't have full control over it. If you were using HttpClient, you could increase the timeout period:

java.net.socketexception: The operation timed out problem in android?

Upvotes: 1

Related Questions