我正在构建一个列表视图,以显示我在资产文件夹中获得的JSON 由于某种原因,我没有得到任何显示在列表视图中,它是空的,我使用相同的代码我用于在线JSON代码我前一段时间做了一些编辑,活动加载正常但没有看到
我的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dua);
BuildDrawer();
RightBuildDrawer();
contactList = new ArrayList<HashMap<String, String>>();
lv = (ListView) findViewById(R.id.dua_listview);
new GetContacts().execute();
}
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getAssets().open("dua.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(DuaActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
try {
JSONObject jsonObj = new JSONObject(loadJSONFromAsset());
contacts = jsonObj.getJSONArray("Duas");
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String id = c.getString("_id");
String category = c.getString("category");
String title = c.getString(TAG_TITLE);
String arabic = c.getString("arabic");
String translation = c.getString(TAG_TRANSLATION);
String transliteration = c.getString(TAG_TRANSLITERATION);
String reference = c.getString(TAG_REFERENCE);
HashMap<String, String> contact = new HashMap<String, String>();
contact.put(TAG_ID, id);
contact.put(TAG_CATEGORY, category);
contact.put(TAG_TITLE, title);
contact.put(TAG_ARABIC, arabic);
contact.put(TAG_TRANSLATION, translation);
contact.put(TAG_TRANSLITERATION, transliteration);
contact.put(TAG_REFERENCE, reference);
contactList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
adapter2 = new SimpleAdapter(
DuaActivity.this, contactList,
R.layout.dua_list_item, new String[] { TAG_CATEGORY, TAG_TITLE},
new int[] { R.id.duaName,R.id.duaCategory});
lv.setAdapter(adapter2);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ENTER HERE
}
});
}
}
这是我的JSON
{
"Duas" : [
{
"_id" : 1,
"category" : "Sleep",
"title" : "When waking up",
"arabic" : "الْحَمْدُ للهِ الَّذِي أَحْيَانَا بَعْدَ مَا أَمَاتَنَا وَإِلَيْهِ النُّشُورُ",
"translation" : "Praise is to Allah Who gives us life after He has caused us to die and to Him is the return.",
"transliteration" : "Alhamdu lillaahil-lathee 'ahyaanaa ba'da maa 'amaatanaa wa'ilayhin-nushoor",
"reference" : "Al-Bukhari, cf. Al-Asqalani, Fathul-Bari 11\/113; Muslim 4\/2083"
},
]
}
logcat ::
07-03 02:45:52.531: W/System.err(14092): org.json.JSONException: Expected ':' after ?? at character 4 of {??
??
07-03 02:45:52.541: W/System.err(14092): ?? ?? ??"??D??u??a??s??"?? ??:?? ??[??
??
07-03 02:45:52.541: W/System.err(14092): ?? ?? ?? ?? ??{??
??
07-03 02:45:52.541: W/System.err(14092): ?? ?? ?? ?? ?? ?? ?? ?? ??"??_??i??d??"?? ??:?? ??1??,??
??
07-03 02:45:52.541: W/System.err(14092): ?? ?? ?? ?? ?? ?? ?? ?? ??"??c??a??t??e??g??o??r??y??"?? ??:?? ??"??S??l??e??e??p??"??,??
??
07-03 02:45:52.541: W/System.err(14092): ?? ?? ?? ?? ?? ?? ?? ?? ??"??t??i??t??l??e??"?? ??:?? ??"??W??h??e??n?? ??w??a??k??i??n??g?? ??u??p??"??,??
??
07-03 02:45:52.541: W/System.err(14092): ?? ?? ?? ?? ?? ?? ?? ?? ??"??a??r??a??b??i??c??"?? ??:?? ??"??'DR-NER/O ??DDGP ??'DNQ0PJ ??#N-RJN'FN' ??(N9R/N ??EN' ??#NEN'*NFN' ??HN%PDNJRGP ??'DFOQ4OH1O"??,??
??
07-03 02:45:52.541: W/System.err(14092): ?? ?? ?? ?? ?? ?? ?? ?? ??"??t??r??a??n??s??l??a??t??i??o??n??"?? ??:?? ??"??P??r??a??i??s??e?? ??i??s?? ??t??o?? ??A??l??l??a??h?? ??W??h??o?? ??g??i??v??e??s?? ??u??s?? ??l??i??f??e?? ??a??f??t??e??r?? ??H??e?? ??h??a??s?? ??c??a??u??s??e??d?? ??u??s?? ??t??o?? ??d??i??e?? ??a??n??d?? ??t??o?? ??H??i??m?? ??i??s?? ??t??h??e?? ??r??e??t??u??r??n??.??"??,??
??
07-03 02:45:52.541: W/System.err(14092): ?? ?? ?? ?? ?? ?? ?? ?? ??"??t??r??a??n??s??l??i??t??e??r??a??t??i??o??n??"?? ??:?? ??"??A??l??h??a??m??d??u?? ??l??i??l??l??a??a??h??i??l??-??l??a??t??h??e??e?? ??'??a??h??y??a??a??n??a??a?? ??b??a??'??d??a?? ??m??a??a?? ??'??a??m??a??a??t??a??n??a??a?? ??w??a??'??i??l??a??y??h??i??n??-??n??u??s??h??o??o??r??"??,??
??
07-03 02:45:52.541: W/System.err(14092): ?? ?? ?? ?? ?? ?? ?? ?? ??"??r??e??f??e??r??e??n??c??e??"?? ??:?? ??"??A??l??-??B??u??k??h??a??r??i??,?? ??c??f??.?? ??A??l??-??A??s??q??a??l??a??n??i??,?? ??F??a??t??h??u??l??-??B??a??r??i?? ??1??1??\??/??1??1??3??;?? ??M??u??s??l??i??m?? ??4??\??/??2??0??8??3??"??
??
07-03 02:45:52.541: W/System.err(14092): ?? ?? ?? ?? ??}??
??
07-03 02:45:52.541: W/System.err(14092): ?? ?? ??]??
??
07-03 02:45:52.541: W/System.err(14092): ??}??
07-03 02:45:52.541: W/System.err(14092): at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
07-03 02:45:52.541: W/System.err(14092): at org.json.JSONTokener.readObject(JSONTokener.java:379)
07-03 02:45:52.541: W/System.err(14092): at org.json.JSONTokener.nextValue(JSONTokener.java:100)
07-03 02:45:52.541: W/System.err(14092): at org.json.JSONObject.<init>(JSONObject.java:155)
07-03 02:45:52.541: W/System.err(14092): at org.json.JSONObject.<init>(JSONObject.java:172)
07-03 02:45:52.541: W/System.err(14092): at com.sahaab.muslimzone.activity.DuaActivity$GetContacts.doInBackground(DuaActivity.java:172)
07-03 02:45:52.541: W/System.err(14092): at com.sahaab.muslimzone.activity.DuaActivity$GetContacts.doInBackground(DuaActivity.java:1)
07-03 02:45:52.541: W/System.err(14092): at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-03 02:45:52.541: W/System.err(14092): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-03 02:45:52.541: W/System.err(14092): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-03 02:45:52.541: W/System.err(14092): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-03 02:45:52.541: W/System.err(14092): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-03 02:45:52.541: W/System.err(14092): at java.lang.Thread.run(Thread.java:841)
答案 0 :(得分:1)
你的JSON在错误的地方有一个逗号:
{
"Duas": [
{
"_id": 1,
"category": "Sleep",
"title": "When waking up",
"arabic": "الْحَمْدُ للهِ الَّذِي أَحْيَانَا بَعْدَ مَا أَمَاتَنَا وَإِلَيْهِ النُّشُورُ",
"translation": "Praise is to Allah Who gives us life after He has caused us to die and to Him is the return.",
"transliteration": "Alhamdu lillaahil-lathee 'ahyaanaa ba'da maa 'amaatanaa wa'ilayhin-nushoor",
"reference": "Al-Bukhari, cf. Al-Asqalani, Fathul-Bari 11/113; Muslim 4/2083"
}, <----- this should not be here
]
}
这是正确的JSON。
{
"Duas": [
{
"_id": 1,
"category": "Sleep",
"title": "When waking up",
"arabic": "الْحَمْدُ للهِ الَّذِي أَحْيَانَا بَعْدَ مَا أَمَاتَنَا وَإِلَيْهِ النُّشُورُ",
"translation": "Praise is to Allah Who gives us life after He has caused us to die and to Him is the return.",
"transliteration": "Alhamdu lillaahil-lathee 'ahyaanaa ba'da maa 'amaatanaa wa'ilayhin-nushoor",
"reference": "Al-Bukhari, cf. Al-Asqalani, Fathul-Bari 11/113; Muslim 4/2083"
}
]
}
答案 1 :(得分:1)
我认为你的代码正在抛出一个JSONException(你正在吞咽,可能已经错过了)。
由于最后一个逗号,JSON不正确:
{
"Duas" : [
{
"_id" : 1,
"category" : "Sleep",
"title" : "When waking up",
"arabic" : "الْحَمْدُ للهِ الَّذِي أَحْيَانَا بَعْدَ مَا أَمَاتَنَا وَإِلَيْهِ النُّشُورُ",
"translation" : "Praise is to Allah Who gives us life after He has caused us to die and to Him is the return.",
"transliteration" : "Alhamdu lillaahil-lathee 'ahyaanaa ba'da maa 'amaatanaa wa'ilayhin-nushoor",
"reference" : "Al-Bukhari, cf. Al-Asqalani, Fathul-Bari 11\/113; Muslim 4\/2083"
}, <-- Remove this
]
}