in LeanbackShowcase/app/src/main/java/androidx/leanback/leanbackshowcase/app/grid/VideoGridExampleFragment.java [170:218]
private void fetchVideosInfo(final String urlString) {
new AsyncTask<Void, Void, FetchResult>() {
@Override
protected void onPostExecute(FetchResult fetchResult) {
if (fetchResult.isSuccess) {
onFetchVideosInfoSuccess(fetchResult.jsonObj);
} else {
onFetchVideosInfoError(fetchResult.exception);
}
}
@Override
protected FetchResult doInBackground(Void... params) {
BufferedReader reader = null;
HttpURLConnection urlConnection = null;
try {
URL url = new URL(urlString);
urlConnection = (HttpURLConnection) url.openConnection();
reader = new BufferedReader(
new InputStreamReader(urlConnection.getInputStream(),
StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
return new FetchResult(new JSONObject(sb.toString()));
} catch (JSONException ex) {
Log.e(TAG, "A JSON error occurred while fetching videos: " + ex.toString());
return new FetchResult(ex);
} catch (IOException ex) {
Log.e(TAG, "An I/O error occurred while fetching videos: " + ex.toString());
return new FetchResult(ex);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (IOException ex) {
Log.e(TAG, "JSON reader could not be closed! " + ex);
}
}
}
}
}.execute();
}