Search code examples
androidhttprequestandroid-internet

How to set connection timeout for JSONParser.makeHttpRequest in Android?


I am getting and posting some data with JSONParser.makeHttpRequest. When the connection is slow it takes a while to get a response and I would like to set the timeout limit to 5 seconds. I found this post showing how to set HttpResponse timeout but couldn't integrate it with JSONParser.

Here is the code I use for HttpRequest

JSONParser jParser = new JSONParser();
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(url, "GET", params);

I will be grateful if I can get a code snippet or a link to another post somewhere else.


Solution

  • You can use Asynctask, and stop the request after some time. See examples here.