Reputation: 617
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
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
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
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:
Upvotes: 1