Created
November 21, 2017 08:46
-
-
Save sanjogshrestha/04c9fbbdc7764a47b0d656f8f0eaf2e2 to your computer and use it in GitHub Desktop.
async doInBackground for JSON
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
@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