This question already has answers here:
Get JSONArray without array name?
(4个答案)
5年前关闭。
很抱歉我的新手问题,但是请告诉我我做错了什么。我正在尝试从包含以下字符串的URL解析JSON:
[{“ Date”:“ 25”,“ Time”:“ 10:00”,“ Place”:“”,“ Text”:“ \ 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!“},{” Date“:” 25“,” Time“:” 10:00“,” Place“:” \ 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 \ u0438- \ u0434 \ u0436 \ u0435 \ u0439 \ u041f \ u0430 \ u0440 \ u043a \ u0430
\ u0414 \ u0417 \ u042e \ u0414 \ u041e DJ \ u0420 \ u043e \ u043c \ u0430 \ u043d
\ u041a \ u0438 \ u0442 \ u0430 \ u0435 \ u0446“}]
如您所见,这是一个没有名称且内部带有对象的JSON数组。那么如何解析它以显示在ListView中呢?我做的事 :
}
编辑:
我已经更改了
之后,如果您遇到任何异常或崩溃,请执行
编辑:1
尝试将此代码用于
Android Error: Window Leaked in AsyncTask
编辑:2
(4个答案)
5年前关闭。
很抱歉我的新手问题,但是请告诉我我做错了什么。我正在尝试从包含以下字符串的URL解析JSON:
[{“ Date”:“ 25”,“ Time”:“ 10:00”,“ Place”:“”,“ Text”:“ \ 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!“},{” Date“:” 25“,” Time“:” 10:00“,” Place“:” \ 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 \ u0438- \ u0434 \ u0436 \ u0435 \ u0439 \ 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)
最佳答案
在您的响应中,第一个元素是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。清理您的项目,然后运行它。Android Error: Window Leaked in AsyncTask
编辑:2
NewsActivity
的第110行是AfishaList.add(contact);
,因此在访问之前创建AfishaList的Object。09-27 12:35