对不起我的新手问题,但请告诉我我做错了什么。我正在尝试从包含以下字符串的URL解析JSON:
[{ “日期”: “25”, “时间”: “10:00”, “放置”: “”, “文本”:“\ u0414 \ u041e \ u0411 \ u0420 \ u041e \ u041f \ u041e \ u0416 \ u0410 \ u041b \ u041e \ u0412 \ u0410 \ u0422 \ u042c \ u0412 \ u00ab \ u041f \ u0410 \ u0420 \ u041a \ u0414 \ u0417 \ u042e \ u0414 \ u041e \ u00bb “},{” 日期! “:” 25" , “时代周刊”: “10:00”, “地方”:“\ u041a \ u0440 \ u044b \ u0448 \ u0430 \ u041b \ u0410 \ u00ab \ u0422 \ u0420 \ u0410 \ u041a \ u0422 \ u041e \ u0420 \ u00bb “ ”文“:” 露天: \ u0432 \ u0430 \ u0441 \ u043f \ u0440 \ u0438 \ u0432 \ u0435 \ u0442 \ u0441 \ u0442 \ u0432 \ u0443 \ u0435 \ u0442 \ u043e \ u0444 \ u0438 \ u0446 \ u0438 \ u0430 \ u043b \ u044c \ u043d \ u044b \ u0439 \ u0434 \ u0434 \ u0434 \ u0435 \ u0435 \ u041f \ u0430 \ u0440 \ u043a \ u0430 \ u0414 \ u0417 \ u042e \ u0414 \ u041e DJ \ u0420 \ u043e \ u043c \ u0430 \ u043d \ u041a \ u0438 \ u0442 \ u0430 \ u0435 \ u0446" }]
正如您所看到的,这是一个没有名称且内部有对象的JSON数组。那么如何解析它以显示在我的ListView中?我做了什么:
public class NewsActivity extends ListActivity {
private ProgressDialog pDialog;
// URL to get contacts JSON
private static String url = "http://mysitehere.com/news.php";
// JSON Node names
private static final String TAG_DATE = "Date";
private static final String TAG_TIME = "Time";
private static final String TAG_PLACE = "Place";
private static final String TAG_TEXT = "Text";;
// Hashmap for ListView
ArrayList<HashMap<String, String>> AfishaList;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.news_activity);
new GetInfo().execute();
}
private class GetInfo extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.d("LOG_TAG","PROGRESS STARTS");
// Showing progress dialog
pDialog = new ProgressDialog(NewsActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
//JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray mJSONArray = new JSONArray(jsonStr);
// looping through All Contacts
for (int i = 0; i < mJSONArray.length(); i++) {
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
JSONObject e = mJSONArray.getJSONObject(i);
// adding each child node to HashMap key => value
contact.put(TAG_DATE, e.getString("Date"));
contact.put(TAG_TIME, e.getString("Time"));
contact.put(TAG_PLACE, e.getString("Place"));
contact.put(TAG_TEXT, e.getString("Text"));
// adding contact to contact list
AfishaList.add(contact);
Log.d("LOG_TAG","SOMETHING IS GOING ON");
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
NewsActivity.this, AfishaList,
R.layout.list_item, new String[] { TAG_DATE, TAG_TIME,
TAG_PLACE, TAG_TEXT }, new int[] { R.id.date,
R.id.time, R.id.place, R.id.text });
setListAdapter(adapter);
Log.w("FINAL","!!!");
}
}
}
修改
我已更改doInBackground
中的方法,现在logcat
向我显示以下日志:
07-31 10:05:35.682 3920-3934/com.project1.2014 E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NullPointerException
at com.project1.2014.NewsActivity$GetInfo.doInBackground(NewsActivity.java:110)
at com.project1.2014.NewsActivity$GetInfo.doInBackground(NewsActivity.java:65)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
答案 0 :(得分:1)
在您的回复中,第一个元素是JSON Array
而不是JSON Object
。这就是为什么你得到JSON Exception
。从您的代码中删除此行
JSONObject jsonObj = new JSONObject(jsonStr);
完成此操作后,如果您收到任何例外或崩溃logcat
。
编辑:1
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing()) {
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
NewsActivity.this, AfishaList,
R.layout.list_item, new String[] { TAG_DATE, TAG_TIME,
TAG_PLACE, TAG_TEXT }, new int[] { R.id.date,
R.id.time, R.id.place, R.id.text });
setListAdapter(adapter);
}
}
尝试onPostExecute(...)
的此代码,如果您显示任何其他Dialog
则忽略该代码。这将导致Windowleak。清理您的项目然后运行它。
编辑:2
NewsActivity
的第110行是AfishaList.add(contact);
所以在访问之前创建AfishaList的对象。
答案 1 :(得分:1)
您的第一个标记为JSON ARRAY
,因此请将其删除。
JSONObject jsonObj = new JSONObject(jsonStr);