以下是我的代码:
private static final String TAG_TYPE = "movie_type";
private static final String TAG_NAME = "movie_name";
private static final String TAG_LENGTH = "movie_length";
private static final String TAG_SCHEDULES = "movie_schedules";
private static final String TAG_CINEMA = "movie_cinema_number";
private static final String TAG_URL = "movie_image_url";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
String readMovieSchedules = readMovieSchedules();
// Hashmap for ListView
ArrayList<HashMap<String, String>> movieList = new ArrayList<HashMap<String, String>>();
JSONArray jsonArray = new JSONArray(readMovieSchedules);
Log.i(MainActivity.class.getName(),
"Number of entries " + jsonArray.length());
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Log.i(MainActivity.class.getName(), jsonObject.getString("movie_name"));
// Storing each json item in variable
String name = jsonObject.getString(TAG_NAME);
String type = jsonObject.getString(TAG_TYPE);
String length = jsonObject.getString(TAG_LENGTH);
String cinema = jsonObject.getString(TAG_CINEMA);
String schedules = jsonObject.getString(TAG_SCHEDULES);
String url = jsonObject.getString(TAG_URL);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_NAME, name);
map.put(TAG_TYPE, type);
map.put(TAG_LENGTH, length);
map.put(TAG_CINEMA, cinema);
map.put(TAG_SCHEDULES, schedules);
map.put(TAG_URL, url);
// adding HashList to ArrayList
movieList.add(map);
//String strURL = TAG_URL.replaceAll(" ", "%20");
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(MainActivity.this, movieList,
R.layout.list_item,
new String[] { TAG_NAME, TAG_CINEMA, TAG_SCHEDULES },
new int[] { R.id.name, R.id.cinema, R.id.schedules });
//new String[] {},
//new int[] {});
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
String cost = ((TextView) view.findViewById(R.id.cinema)).getText().toString();
String description = ((TextView) view.findViewById(R.id.schedules)).getText().toString();
//String url = ((TextView) view.findViewById(R.id.image_)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(TAG_NAME, name);
in.putExtra(TAG_CINEMA, cost);
in.putExtra(TAG_SCHEDULES, description);
//in.putExtra(TAG_URL, url);
startActivity(in);
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
此代码正在使用SDK 8,但不能正常使用17.我遇到了这个问题。这里有谁知道如何调试这个?我很乐意感谢你的帮助。感谢。
答案 0 :(得分:2)
自API 11(3.0)起,您无法在主线程上进行网络调用。在这里阅读更多: http://www.androiddesignpatterns.com/2012/06/app-force-close-honeycomb-ics.html
答案 1 :(得分:1)
你必须将你的代码放在AsyncTask内,因为我相信API 14,所以禁止在主UI线程上下载任何内容。
这将是这样的:
public class someTask extends AsyncTask<String, Void, String> {
public MainActivity activity;
public someTask(MainActivity a)
{
activity = a;
}
@Override
protected String doInBackground(String... urls) {
String stringtoparse=null;
for (String url : urls) {
stringtoparse= readMovieSchedules(url); // getting XML from URL
}
return stringtoparse;
}
@Override
protected void onPreExecute(){
}
@Override
protected void onPostExecute(String readMovieSchedules) {
// Hashmap for ListView
ArrayList<HashMap<String, String>> movieList = new ArrayList<HashMap<String, String>>();
JSONArray jsonArray = new JSONArray(readMovieSchedules);
Log.i(MainActivity.class.getName(),
"Number of entries " + jsonArray.length());
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Log.i(MainActivity.class.getName(), jsonObject.getString("movie_name"));
// Storing each json item in variable
String name = jsonObject.getString(TAG_NAME);
String type = jsonObject.getString(TAG_TYPE);
String length = jsonObject.getString(TAG_LENGTH);
String cinema = jsonObject.getString(TAG_CINEMA);
String schedules = jsonObject.getString(TAG_SCHEDULES);
String url = jsonObject.getString(TAG_URL);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_NAME, name);
map.put(TAG_TYPE, type);
map.put(TAG_LENGTH, length);
map.put(TAG_CINEMA, cinema);
map.put(TAG_SCHEDULES, schedules);
map.put(TAG_URL, url);
// adding HashList to ArrayList
movieList.add(map);
}
}
你必须修改readMovieSchedules()以将url作为参数,就像这个readMovieSchedules(url)一样,我认为它会正常工作,你可以像这样调用任务:
getMovieSched task = new someTask(MainActivity.this);
task.execute(url);
答案 2 :(得分:1)
ASyncTask专门针对这样的场景。它允许您通过网络以单独的线程进行通信,而无需使用线程。
ASyncTask有以下方法:
onPreExecute():在主处理发生之前调用它...一次使用它就是启动进度对话框,通知用户该事务。
doInBackground():这是您从网络执行数据交换的地方。没有UI在这里摆弄。
onProgressUpdate():可用于通知事务进度
onPostExecute():关闭progressDialogs / bars并从提取的数据中更新你的视图!
另一个解决方案是使用runOnUiThread(),但不鼓励这样做!
class nwthread extends AsyncTask<Void,Void,Void>
{
@Override
protected void onPreExecute() {
//progress dialog invoke ( notifies the user about whats going on better than making them stare on a blank screen)
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
//make http request/parse json here
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
//dismiss progress dialog
// update the UI here ... ie declare adapters / bind them, update other views
}
}
最后调用nwthread.execute();在setContentView();
之后