Created
July 19, 2016 19:00
-
-
Save alvareztech/b727735e3ca9cbb19d6b3729310eeed4 to your computer and use it in GitHub Desktop.
Método para obtener JSON desde una URL en Android.
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
public String getJsonFromUrl(String urlString) { | |
StringBuilder result = new StringBuilder(); | |
HttpURLConnection urlConnection = null; | |
try { | |
URL url = new URL(urlString); | |
urlConnection = (HttpURLConnection) url.openConnection(); | |
InputStream in = new BufferedInputStream(urlConnection.getInputStream()); | |
BufferedReader reader = new BufferedReader(new InputStreamReader(in)); | |
String line; | |
while ((line = reader.readLine()) != null) { | |
result.append(line); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
assert urlConnection != null; | |
urlConnection.disconnect(); | |
} | |
return result.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment