我正在制作一个应用程序来显示来自频道的视频。目前,我正在使用ListView显示视频的标题。每当我开始活动VideoPlay时,应用程序崩溃。我的代码出了什么问题?
代码:
package com.aer.illbehonest;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class VideoPlay extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_play);
ListView listView = (ListView) findViewById(R.id.videolist);
String[] values = null;
try {
VideoData.getTitle(values);
} catch (IOException e) {
e.printStackTrace();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
}
}
代码:
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import org.apache.commons.io.IOUtils;
public class VideoData {
public static String[] getTitle(String[] title) throws IOException {
URL jsonURL = new URL("http://gdata.youtube.com/feeds/api/users/illbehonest/uploads?v=2&alt=jsonc");
URLConnection jc = jsonURL.openConnection();
InputStream is = jc.getInputStream();
String jsonTxt = IOUtils.toString(is);
JSONObject json = (JSONObject) JSONSerializer.toJSON(jsonTxt);
JSONObject jdata = json.getJSONObject("data");
JSONArray jentry = jdata.getJSONArray("items");
for (int entryNumber = 0; entryNumber<=25; entryNumber++){
JSONObject entry = jentry.getJSONObject(entryNumber);
String[] jtitle = {entry.getString("title")};
title = jtitle;
}
return title;
}
}
答案 0 :(得分:1)
您需要使用async task拨打网络电话。
这里有一个ListView示例: http://android-er.blogspot.co.uk/2010/07/load-listview-in-background-asynctask.html