Skip to content

Instantly share code, notes, and snippets.

@jpukg
Created October 6, 2016 21:23
Show Gist options
  • Select an option

  • Save jpukg/ccc8ea0d71067592ef507ba75c5ce4b3 to your computer and use it in GitHub Desktop.

Select an option

Save jpukg/ccc8ea0d71067592ef507ba75c5ce4b3 to your computer and use it in GitHub Desktop.
httpUrlconnection helper class
package com.example.ikram;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Http {
public static String read(String httpUrl) throws IOException {
String httpData = "";
InputStream inputStream = null;
HttpURLConnection httpURLConnection = null;
try {
URL url = new URL(httpUrl);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.connect();
inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuffer stringBuffer = new StringBuffer();
String line = "";
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
}
httpData = stringBuffer.toString();
bufferedReader.close();
} catch (Exception e) {
Log.d("Exception - reading Http url", e.toString());
} finally {
inputStream.close();
httpURLConnection.disconnect();
}
return httpData;
}
}
//Example
String response = Http.read("http://www.google.com");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment