Skip to content

Instantly share code, notes, and snippets.

@rms1000watt
Created May 31, 2016 23:02
Show Gist options
  • Save rms1000watt/8e4fc62a5ef0715388bc83438838c49c to your computer and use it in GitHub Desktop.
Save rms1000watt/8e4fc62a5ef0715388bc83438838c49c to your computer and use it in GitHub Desktop.
Send HTTP Request on Android
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
private void sendHTTPRequest() {
try {
String data = "";
String webPage = "";
URL url = new URL("http://www.google.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
while ((data = reader.readLine()) != null) {
webPage += data + "\n";
}
Log.i("WebPage", webPage);
urlConnection.disconnect();
}
catch (Exception e) {
Log.e("URL", e.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment