Skip to content

Instantly share code, notes, and snippets.

@sanjogshrestha
Created November 21, 2017 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sanjogshrestha/04c9fbbdc7764a47b0d656f8f0eaf2e2 to your computer and use it in GitHub Desktop.
Save sanjogshrestha/04c9fbbdc7764a47b0d656f8f0eaf2e2 to your computer and use it in GitHub Desktop.
async doInBackground for JSON
@Override
protected String doInBackground(String... strings) {
try {
URL mUrl = new URL(strings[0]);
HttpURLConnection httpConnection = (HttpURLConnection) mUrl.openConnection();
httpConnection.setRequestMethod("GET");
httpConnection.setUseCaches(false);
httpConnection.setAllowUserInteraction(false);
httpConnection.connect();
int responseCode = httpConnection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
BufferedReader br = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
br.close();
return sb.toString();
}else
return null;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment