I am working on an app that fetches youtube videos from two channels using the v3 api. I fire two queries: one to get a list of videos & the other to get a list of the video details (duration and views). Im doing these using an asynctask but the task does not complete at first attempt. I have to exit the app and then re-open before the list is displayed. Can anyone tell me why this might be happening and if there is anything wrong with the way I have implemented the asynctask? Below is my code for the Asynctask.
private class Fetchlist extends AsyncTask<String, String, JSONArray> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(VideoListDemoActivity.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONArray doInBackground(String... params) {
JSONArray jsonArray1 = null;
JSONArray jsonArray2 = null;
JSONArray jsonArrayFinal = null;
try {
String urlvideos1 = "https://www.googleapis.com/youtube/v3/search?key=xyz&channelId=abc&part=snippet&order=viewCount&maxResults=20";
String urlvideos2 = "https://www.googleapis.com/youtube/v3/search?key=xyz2&channelId=abc2&part=snippet&order=viewCount&maxResults=20";
JSONParser jParser = new JSONParser();
JSONObject jsonVideos1 = jParser.getJSONFromUrl(urlvideos1);
JSONObject jsonVideos2 = jParser.getJSONFromUrl(urlvideos2);
jsonArray1 = jsonVideos1.getJSONArray("items");
jsonArray2 = jsonVideos2.getJSONArray("items");
jsonArrayFinal = concatArray(jsonArray1, jsonArray2);
for (int i = 0; i < jsonArrayFinal.length(); i++) {
JSONObject jsonID = jsonArrayFinal.getJSONObject(i);
Log.d("Async Values", "inside do in background");
try {
JSONObject jsonVid = jsonID.getJSONObject("id");
JSONObject jsonSnippet = jsonID
.getJSONObject("snippet");
String title = jsonSnippet.getString("title");
String videoid = jsonVid.getString("videoId");
try {
String urltwo = "https://www.googleapis.com/youtube/v3/videos?id="
+ videoid
+ "&key=xyz&part=snippet,contentDetails,statistics,status";
JSONParser jParsertwo = new JSONParser();
JSONObject jsontwo = jParsertwo
.getJSONFromUrl(urltwo);
// JSONObject jsonID = json.getJSONObject("items");
JSONArray jsonArraytwo = jsontwo
.getJSONArray("items");
JSONObject jsonIDtwo = jsonArraytwo
.getJSONObject(0);
JSONObject jsonView = jsonIDtwo
.getJSONObject("statistics");
JSONObject jsonDuration = jsonIDtwo
.getJSONObject("contentDetails");
String Duration = jsonDuration
.getString("duration");
String strDuration = Duration;
SimpleDateFormat df = new SimpleDateFormat(
"'PT'mm'M'ss'S'");
String youtubeDuration = Duration;
Date d = df.parse(youtubeDuration);
Calendar c = new GregorianCalendar();
c.setTime(d);
c.setTimeZone(TimeZone.getDefault());
int minduration = c.get(Calendar.MINUTE);
int secduration = c.get(Calendar.SECOND);
String strMin = String.valueOf(minduration);
String strSec = String.valueOf(secduration);
// Toast.makeText(VideoListDemoActivity.this,
// strMin, Toast.LENGTH_LONG).show();
String viewcount = jsonView.getString("viewCount");
// Toast.makeText(VideoListDemoActivity.this,
// viewcount, Toast.LENGTH_LONG).show();
title = jsonSnippet.getString("title")
+ "\n\nViews: " + viewcount + " Length: "
+ strMin + ":" + strSec;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("JSON msg", e.toString());
}
if (!list.contains(title)) {
list.add(new VideoEntry(title, videoid));
}
}
catch (Exception e) {
Log.d("Do in background", e.toString());
}
}
} catch (Exception e) {
Log.d("Do in background", e.toString());
}
return jsonArrayFinal;
}
protected void onPostExecute(JSONArray jsonArrayFinal) {
pDialog.dismiss();
layout();
}
}
Below is the layout code. Its part of the youtube api demo and implements fragments.
private void layout() {
boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
listFragment.getView().setVisibility(
isFullscreen ? View.GONE : View.VISIBLE);
listFragment.setLabelVisibility(isPortrait);
closeButton.setVisibility(isPortrait ? View.VISIBLE : View.GONE);
if (isFullscreen) {
videoBox.setTranslationY(0); // Reset any translation that was
// applied in portrait.
setLayoutSize(videoFragment.getView(), MATCH_PARENT, MATCH_PARENT);
setLayoutSizeAndGravity(videoBox, MATCH_PARENT, MATCH_PARENT,
Gravity.TOP | Gravity.LEFT);
} else if (isPortrait) {
setLayoutSize(listFragment.getView(), MATCH_PARENT, MATCH_PARENT);
setLayoutSize(videoFragment.getView(), MATCH_PARENT, WRAP_CONTENT);
setLayoutSizeAndGravity(videoBox, MATCH_PARENT, WRAP_CONTENT,
Gravity.BOTTOM);
} else {
videoBox.setTranslationY(0); // Reset any translation that was
// applied in portrait.
int screenWidth = dpToPx(getResources().getConfiguration().screenWidthDp);
setLayoutSize(listFragment.getView(), screenWidth / 4, MATCH_PARENT);
int videoWidth = screenWidth - screenWidth / 4
- dpToPx(LANDSCAPE_VIDEO_PADDING_DP);
setLayoutSize(videoFragment.getView(), videoWidth, WRAP_CONTENT);
setLayoutSizeAndGravity(videoBox, videoWidth, WRAP_CONTENT,
Gravity.RIGHT | Gravity.CENTER_VERTICAL);
}
}