get Url content with max retries : Url « Network « Android

get Url content with max retries


import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

class Main {
  private static HttpParams defaultHttpParams = new BasicHttpParams();
  public static String getUrl(String url, int maxRetries) throws IOException {
    String result = null;
    int retries = 0;
    DefaultHttpClient httpclient = new DefaultHttpClient(defaultHttpParams);
    HttpGet httpget = new HttpGet(url);
    while (retries <= maxRetries && result == null) {
      try {
        HttpEntity entity = httpclient.execute(httpget).getEntity();

        if (entity != null) {
          result = EntityUtils.toString(entity).trim();
      } catch (SocketException se) {
        if (retries > maxRetries) {
          throw se;
        } else {
          //Log.v(TAG, "SocketException, retrying " + retries);

    return result;


Related examples in the same category

1.Using Intent to open a URL
2.Process xml document from a Url
3.Suggest Url Provider
4.Showing android:autoLink property, which linkifies things like URLs and phone numbers found in the text.
5.extends ArrayAdapter to create URL history
6.Used to compress URL using the service.
7.URL encode and decode
8.Is valid URL
9.Download from URLConnection
10.Request from an URL
11.Get Url From Img Tag
12.Returns the contents of the given URL as an array of strings
13.Read from a URL
14.Pull the raw text content of the given URL.
15.Get Video from URL
16.Gets data from URL
17.get Url Response
18.URL Encode Utils
19.Downloads a file given URL to specified destination
20.get Host From Url
21.encode Url
22.decode Url
23.Convert a byte array to a URL encoded string
24.get Url By Post
25.Take a base url and a {@link Map} of parameters to build a valid url