开发者

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.


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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜