Mega Code Archive

 
Categories / Android / Network
 

Get Url Response

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; class IOUtils {   private static final String LOG_TAG = "IOUtils";   public static final String PREFS_FILE = "javaeye.prefs";   public static String getUrlResponse(String url) {     try {       HttpGet get = new HttpGet(url);       HttpClient client = new DefaultHttpClient();       HttpResponse response = client.execute(get);       HttpEntity entity = response.getEntity();       return convertStreamToString(entity.getContent());     } catch (Exception e) {     }     return null;   }   private static String convertStreamToString(InputStream is) {     BufferedReader reader = new BufferedReader(new InputStreamReader(is),8 * 1024);     StringBuilder sb = new StringBuilder();     String line = null;     try {       while ((line = reader.readLine()) != null) {         sb.append(line + "\n");       }     } catch (IOException e) {     } finally {       try {         is.close();       } catch (IOException e) {       }     }     return sb.toString();   } }