Created
October 6, 2016 21:23
-
-
Save jpukg/ccc8ea0d71067592ef507ba75c5ce4b3 to your computer and use it in GitHub Desktop.
httpUrlconnection helper class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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