我一直在努力开发一个应用,在点击后,会打开一个新活动并从网址加载数据。 这是新的活动代码
ProgressDialog对话框;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
private class MyTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
dialog.setMessage("Processing");
dialog.setIndeterminate(true);
dialog.show();
dialog.getWindow().setLayout(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
try {
JSONObject jsonObject = new JSONObject();
String url = " http://www.trailermag.com/tsappapis/?request=featuredAdList";
JSONArray trailersJSON = jsonObject.getJSONArray(url);
for (int i = 0; i < trailersJSON.length(); i++) {
Trail aTrail = new Trail();
JSONObject contactObject = trailersJSON.getJSONObject(i);
aTrail.id = contactObject.getString(V_Id);
aTrail.image = contactObject.getString(V_Image);
aTrail.title = contactObject.getString(V_Title);
aTrail.price = contactObject.getString(V_Price);
webData.add(aTrail);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if (dialog.isShowing()) {
System.out.println("IN POST EXE");
dialog.dismiss();
}
}
}
答案 0 :(得分:-1)
尝试将此代码替换为您的代码后,这将有效,我已对其进行了测试。
JSONArray mJsonArray = new JSONArray(response);
for (int i = 0; i < mJsonArray.length(); i++) {
JSONObject mJsonObject = mJsonArray.getJSONObject(i);
String idStr = mJsonObject.getString("id");
String imageStr = mJsonObject.getString("image");
String titleStr = mJsonObject.getString("title");
String priceStr = mJsonObject.getString("price");
}
... Happeee编程....