我已在我的应用中集成了youtube频道,但它没有显示所有视频..对于某些频道,它只显示非常少的视频..如果我在抓住JSONException时放置throw new RuntimeException(e);
应用崩溃并显示以下内容错误 -
11-02 20:00:40.868: E/AndroidRuntime(15351): FATAL EXCEPTION: AsyncTask #1
11-02 20:00:40.868: E/AndroidRuntime(15351): java.lang.RuntimeException: An error occured while executing doInBackground()
11-02 20:00:40.868: E/AndroidRuntime(15351): at android.os.AsyncTask$3.done(AsyncTask.java:299)
11-02 20:00:40.868: E/AndroidRuntime(15351): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
11-02 20:00:40.868: E/AndroidRuntime(15351): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
11-02 20:00:40.868: E/AndroidRuntime(15351): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
11-02 20:00:40.868: E/AndroidRuntime(15351): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-02 20:00:40.868: E/AndroidRuntime(15351): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-02 20:00:40.868: E/AndroidRuntime(15351): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-02 20:00:40.868: E/AndroidRuntime(15351): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-02 20:00:40.868: E/AndroidRuntime(15351): at java.lang.Thread.run(Thread.java:856)
11-02 20:00:40.868: E/AndroidRuntime(15351): Caused by: java.lang.RuntimeException: org.json.JSONException: No value for videoId
11-02 20:00:40.868: E/AndroidRuntime(15351): at com.wingnity.jsonparsingtutorial.SoundcloudActivity$JSONAsyncTask.doInBackground(SoundcloudActivity.java:223)
11-02 20:00:40.868: E/AndroidRuntime(15351): at com.wingnity.jsonparsingtutorial.SoundcloudActivity$JSONAsyncTask.doInBackground(SoundcloudActivity.java:1)
11-02 20:00:40.868: E/AndroidRuntime(15351): at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-02 20:00:40.868: E/AndroidRuntime(15351): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-02 20:00:40.868: E/AndroidRuntime(15351): ... 5 more
11-02 20:00:40.868: E/AndroidRuntime(15351): Caused by: org.json.JSONException: No value for videoId
11-02 20:00:40.868: E/AndroidRuntime(15351): at org.json.JSONObject.get(JSONObject.java:354)
11-02 20:00:40.868: E/AndroidRuntime(15351): at org.json.JSONObject.getString(JSONObject.java:510)
11-02 20:00:40.868: E/AndroidRuntime(15351): at com.wingnity.jsonparsingtutorial.SoundcloudActivity$JSONAsyncTask.doInBackground(SoundcloudActivity.java:191)
11-02 20:00:40.868: E/AndroidRuntime(15351): ... 8 more
11-02 20:00:40.952: E/AppErrorDialog(348): Failed to get ILowStorageHandle instance
11-02 20:00:41.476: E/WindowManager(15351): Activity com.wingnity.jsonparsingtutorial.SoundcloudActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41929000 that was originally added here
11-02 20:00:41.476: E/WindowManager(15351): android.view.WindowLeaked: Activity com.wingnity.jsonparsingtutorial.SoundcloudActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41929000 that was originally added here
11-02 20:00:41.476: E/WindowManager(15351): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:418)
11-02 20:00:41.476: E/WindowManager(15351): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:294)
11-02 20:00:41.476: E/WindowManager(15351): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:226)
这是我想要整合的频道 -
http://www.youtube.com/user/BeingIndianchannel
它只显示最新的三个视频 这是代码 -
ArrayList<Channel> actorsList;
//private static String url = "https://www.googleapis.com/youtube/v3/search?key=xyz&channelId=UCdf6i1jTJl0TLzXc1KMDMKA&part=snippet,id&order=date&maxResults=20";
String videoid;
// JSON Node names
private static final String TAG_ITEMS = "items";
private static final String TAG_KIND = "kind";
private static final String TAG_ETAG = "etag";
private static final String TAG_ID = "id";
private static final String TAG_KINDV = "kind";
private static final String TAG_VIDEOID = "videoId";
private static final String TAG_SNIPPET = "snippet";
private static final String TAG_PUBLISHED = "publishedAt";
private static final String TAG_CHANNELID = "channelId";
private static final String TAG_TITLE = "title";
private static final String TAG_DESCRIPTION = "description";
private static final String TAG_THUMBNAILS = "thumbnails";
private static final String TAG_DEFAULT = "default";
private static final String TAG_URL = "url";
private static final String TAG_MEDIUM = "medium";
private static final String TAG_URL2 = "url";
private static final String TAG_HIGH = "high";
private static final String TAG_URL3 = "url";
private static final String TAG_CHANNEL = "channelTitle";
private static final String TAG_BROADCAST = "liveBroadcastContent";
ChannelAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.youtubevidlist);
actorsList = new ArrayList<Channel>();
new JSONAsyncTask().execute("https://www.googleapis.com/youtube/v3/search?key=xyz&channelId=UChAlpEbfW1y9auiBC08ZwGQ&part=snippet,id&order=date&maxResults=20");
ListView listview = (ListView)findViewById(R.id.list);
adapter = new ChannelAdapter(getApplicationContext(), R.layout.row, actorsList);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long id) {
// TODO Auto-generated method stub
String name = ((TextView) arg1.findViewById(R.id.name_label))
.getText().toString();
Intent in = new Intent(getApplicationContext(),
YoutubeActivity2.class);
in.putExtra("TAG_VIDEOID", name);
startActivity(in);
;
}
});
}
class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
ProgressDialog dialog;
@Override
protected void onPreExecute()
{
super.onPreExecute();
dialog = new ProgressDialog(SoundcloudActivity.this);
dialog.setMessage("Loading, please wait");
dialog.setTitle("Connecting server");
dialog.show();
dialog.setCancelable(false);
}
@Override
protected Boolean doInBackground(String... urls) {
try {
//------------------>>
HttpGet httppost = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
// StatusLine stat = response.getStatusLine();
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsono = new JSONObject(data);
JSONArray jarray = jsono.getJSONArray(TAG_ITEMS);
for (int i = 0; i < jarray.length(); i++) {
JSONObject c = jarray.getJSONObject(i);
Channel actor = new Channel();
actor.setKind(c.getString(TAG_KIND));
actor.setEtag(c.getString(TAG_ETAG));
JSONObject id = c.getJSONObject(TAG_ID);
actor.setKind2(id.getString(TAG_KINDV));
actor.setVideoid(id.getString(TAG_VIDEOID));
JSONObject snippet = c.getJSONObject(TAG_SNIPPET);
actor.setPublished(snippet.getString(TAG_PUBLISHED));
actor.setChannelid(snippet.getString(TAG_CHANNELID));
actor.setTitle(snippet.getString(TAG_TITLE));
actor.setDescription(snippet.getString(TAG_DESCRIPTION));
JSONObject thumbnail = snippet.getJSONObject(TAG_THUMBNAILS);
JSONObject defaul = thumbnail.getJSONObject(TAG_DEFAULT);
actor.setUri1(defaul.getString(TAG_URL));
JSONObject medium = thumbnail.getJSONObject(TAG_MEDIUM);
actor.setUri2(medium.getString(TAG_URL2));
JSONObject high = thumbnail.getJSONObject(TAG_HIGH);
actor.setUri3(high.getString(TAG_URL3));
actor.setChannel(snippet.getString(TAG_CHANNEL));
actor.setBroadcast(snippet.getString(TAG_BROADCAST));
actorsList.add(actor);
}
return true;
}
//------------------>>
} catch (ParseException e1) {
e1.printStackTrace();
Log.e("exception","ParseException");
} catch (IOException e) {
e.printStackTrace();
Log.e("exception","IOException");
} catch (JSONException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
return false;
}
protected void onPostExecute(Boolean result) {
dialog.cancel();
adapter.notifyDataSetChanged();
if(result == false)
Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
}
}
}
答案 0 :(得分:1)
Caused by: org.json.JSONException: No value for videoId
根据the API:
如果id.type属性的值为youtube#video,则此属性将出现,其值将包含YouTube用于唯一标识与搜索查询匹配的视频的ID。
videoId
是可选值。在调用JSONObject.has()
之前,您需要使用JSONObject.getString()
检查它是否存在。