Skip to content

Instantly share code, notes, and snippets.

@alvareztech
Created July 19, 2016 19:00
Show Gist options
  • Save alvareztech/b727735e3ca9cbb19d6b3729310eeed4 to your computer and use it in GitHub Desktop.
Save alvareztech/b727735e3ca9cbb19d6b3729310eeed4 to your computer and use it in GitHub Desktop.
Método para obtener JSON desde una URL en Android.
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