Last active
March 5, 2016 13:39
-
-
Save wuapnjie/01ad86f6cf448157d2e3 to your computer and use it in GitHub Desktop.
Normal network connection
This file contains 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
try { | |
URL url = new URL("<api call>"); | |
urlConnection = (HttpURLConnection) url.openConnection(); | |
urlConnection.setRequestMethod("GET"); | |
urlConnection.connect(); | |
InputStream inputStream = urlConnection.getInputStream(); | |
StringBuffer buffer = new StringBuffer(); | |
if (inputStream == null) { | |
// Nothing to do. | |
return null; | |
} | |
reader = new BufferedReader(new InputStreamReader(inputStream)); | |
String line; | |
while ((line = reader.readLine()) != null) { | |
buffer.append(line + "\n"); | |
} | |
if (buffer.length() == 0) { | |
return null; | |
} | |
result = buffer.toString(); | |
} catch (IOException e) { | |
Log.e("Request", "Error ", e); | |
return null; | |
} finally{ | |
if (urlConnection != null) { | |
urlConnection.disconnect(); | |
} | |
if (reader != null) { | |
try { | |
reader.close(); | |
} catch (final IOException e) { | |
Log.e("Request", "Error closing stream", e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment