user422688
user422688

Reputation: 617

Time out exception when invoke rest webservice in android

I am trying to call rest web services in andoid, but i have always a time out exception. The code is:

HttpClient httpclient = new DefaultHttpClient();  

HttpGet request = new HttpGet("http://XXXX:8080/YYY/zzzz?username=0001epd&password=1111");

ResponseHandler<String> handler = new BasicResponseHandler();  
try {  
        String result = httpclient.execute(request, handler);  
} catch (ClientProtocolException e) {  
           e.printStackTrace();  
} catch (IOException e) {  
          e.printStackTrace();  
           }  
httpclient.getConnectionManager().shutdown();  

Do you have any idea where is the error?

In the manifest I added android.permission.INTERNET

Upvotes: 1

Views: 3954

Answers (3)

prashant
prashant

Reputation: 3608

If you are running this on emulator, did you try putting the above url on emulator browser and see if you get the result. If you can see the result in browser and you are behind proxy then try adding the below lines in your code

HttpParams my_httpParams = new BasicHttpParams(); 

final String proxyHost = android.net.Proxy.getDefaultHost();

final int proxyPort = android.net.Proxy.getDefaultPort();

if(proxyPort != -1)
{
  my_httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(proxyHost, proxyPort));            
}

Pass the HttpParams in DefaultHttpClient

Upvotes: 1

Impression
Impression

Reputation: 727

Maybe your internet connection at your mobile device or work station is to slow or the remote service takes some time. You can test it in using your internet browser for requesting the REST url.

At least you can try to increase the timeouts in your code:

final int timeout = 10000; //ms
HttpConnectionParams.setConnectionTimeout(httpParams, timeout);
HttpConnectionParams.setSoTimeout(httpParams, timeout);
final HttpClient httpclient = new DefaultHttpClient(httpParams);

Upvotes: 1

Yoni Samlan
Yoni Samlan

Reputation: 38075

I doubt it's the kind of timeout you can fix by just upping the limit as Impression suggests, this sounds more like a networking problem with your setup to reach the server.

Is this from an emulator or a real device? Make sure you can actually reach the server (eg see what entering XXXX:8080 in the Browser does) ; if you're on the emulator trying to connect to your local machine you need to use 10.0.2.2 as the IP address (not 127.0.0.1 or your IP on the LAN). And if you're on a physical device, the usual client-server network check rules apply:

  • Are you on the same LAN/subnet as your server, if it's a local development server?
  • If you're trying to make the connection externally over the Internet and not from your LAN, are you sure all the right firewall ports are opened up to make the external request from your development server?
  • Is your server software set up to accept connections from external requests? (Some development servers, like Django, are set up to only accept localhost connections by default for security reasons).

Upvotes: 1

Related Questions