0
我試圖通過php
文件從遠程database
獲取數據,並將內容輸出到ListView
容器中。當使用測試數據(填充字符串的數據和循環一個JSON
對象的創建,然後添加每一個對象到JSONArray
)一切工作正常。所以我知道,誤差必須在下面的代碼:針對排除php交互的不兼容JSON類型的接收錯誤
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
String name = jsonResponse.getString("name");
String genreAddress = jsonResponse.getString("genreAddress");
String blurb = jsonResponse.getString("blurb");
try {
JSONObject newListEntity = new JSONObject();
newListEntity.put("name", name);
newListEntity.put("genreAddress", genreAddress);
newListEntity.put("blurb", blurb);
jsonArray.put(newListEntity);
} catch (JSONException e) {
e.printStackTrace();
}
ArrayList<GetDiscoverContent> getPanelContent = GetDiscoverContent.fromJSON(jsonArray);
DiscoverAdapter adapter = new DiscoverAdapter(getContext(), getPanelContent);
listView.setAdapter(adapter);
} catch(JSONException e) {
e.printStackTrace();
}
}
};
DiscoverDbRequest dbReq = new DiscoverDbRequest(tableType, responseListener);
RequestQueue queue = Volley.newRequestQueue(getContext());
queue.add(dbReq);
下面是錯誤的logcat:
02-24 11:44:40.389 20391-20391/com.socialivemusic.socialivemusic
W/System.err: org.json.JSONException: Value [] of type
org.json.JSONArray cannot be converted to JSONObject
02-24 11:44:40.389 20391-20391/com.socialivemusic.socialivemusic W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
02-24 11:44:40.389 20391-20391/com.socialivemusic.socialivemusic W/System.err: at org.json.JSONObject.<init>(JSONObject.java:159)
02-24 11:44:40.399 20391-20391/com.socialivemusic.socialivemusic W/System.err: at org.json.JSONObject.<init>(JSONObject.java:172)
02-24 11:44:40.409 20391-20391/com.socialivemusic.socialivemusic W/System.err: at
com.socialivemusic.socialivemusic.DiscoverFragment$1.onResponse(DiscoverFragment.java:96)
02-24 11:44:40.409 20391-20391/com.socialivemusic.socialivemusic W/System.err: at
com.socialivemusic.socialivemusic.DiscoverFragment$1.onResponse(DiscoverFragment.java:92)
02-24 11:44:40.409 20391-20391/com.socialivemusic.socialivemusic W/System.err: at
com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
02-24 11:44:40.419 20391-20391/com.socialivemusic.socialivemusic W/System.err: at
com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
02-24 11:44:40.429 20391-20391/com.socialivemusic.socialivemusic W/System.err: at
com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
02-24 11:44:40.429 20391-20391/com.socialivemusic.socialivemusic W/System.err: at
android.os.Handler.handleCallback(Handler.java:733)
02-24 11:44:40.439 20391-20391/com.socialivemusic.socialivemusic W/System.err: at
android.os.Handler.dispatchMessage(Handler.java:95)
02-24 11:44:40.439 20391-20391/com.socialivemusic.socialivemusic W/System.err: at android.os.Looper.loop(Looper.java:136)
02-24 11:44:40.439 20391-20391/com.socialivemusic.socialivemusic W/System.err: at
android.app.ActivityThread.main(ActivityThread.java:5021)
02-24 11:44:40.449 20391-20391/com.socialivemusic.socialivemusic W/System.err: at java.lang.reflect.Method.invokeNative(Native
Method)
02-24 11:44:40.449 20391-20391/com.socialivemusic.socialivemusic W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
02-24 11:44:40.459 20391-20391/com.socialivemusic.socialivemusic W/System.err: at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
02-24 11:44:40.459 20391-20391/com.socialivemusic.socialivemusic W/System.err: at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
02-24 11:44:40.459 20391-20391/com.socialivemusic.socialivemusic W/System.err: at dalvik.system.NativeStart.main(Native Method)
02-24 11:44:41.069 20391-20391/com.socialivemusic.socialivemusic W/System.err: org.json.JSONException: Value [] of type
org.json.JSONArray cannot be converted to JSONObject
02-24 11:44:41.089 20391-20391/com.socialivemusic.socialivemusic W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
02-24 11:44:41.089 20391-20391/com.socialivemusic.socialivemusic W/System.err: at org.json.JSONObject.<init>(JSONObject.java:159)
02-24 11:44:41.099 20391-20391/com.socialivemusic.socialivemusic W/System.err: at org.json.JSONObject.<init>(JSONObject.java:172)
02-24 11:44:41.099 20391-20391/com.socialivemusic.socialivemusic W/System.err: at
com.socialivemusic.socialivemusic.DiscoverFragment$1.onResponse(DiscoverFragment.java:96)
02-24 11:44:41.109 20391-20391/com.socialivemusic.socialivemusic W/System.err: at
com.socialivemusic.socialivemusic.DiscoverFragment$1.onResponse(DiscoverFragment.java:92)
02-24 11:44:41.109 20391-20391/com.socialivemusic.socialivemusic W/System.err: at
com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
02-24 11:44:41.109 20391-20391/com.socialivemusic.socialivemusic W/System.err: at
com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
02-24 11:44:41.119 20391-20391/com.socialivemusic.socialivemusic W/System.err: at
com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
02-24 11:44:41.119 20391-20391/com.socialivemusic.socialivemusic W/System.err: at
android.os.Handler.handleCallback(Handler.java:733)
02-24 11:44:41.129 20391-20391/com.socialivemusic.socialivemusic W/System.err: at
android.os.Handler.dispatchMessage(Handler.java:95)
02-24 11:44:41.129 20391-20391/com.socialivemusic.socialivemusic W/System.err: at android.os.Looper.loop(Looper.java:136)
02-24 11:44:41.139 20391-20391/com.socialivemusic.socialivemusic W/System.err: at
android.app.ActivityThread.main(ActivityThread.java:5021)
02-24 11:44:41.139 20391-20391/com.socialivemusic.socialivemusic W/System.err: at java.lang.reflect.Method.invokeNative(Native
Method)
02-24 11:44:41.139 20391-20391/com.socialivemusic.socialivemusic W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
02-24 11:44:41.149 20391-20391/com.socialivemusic.socialivemusic W/System.err: at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
02-24 11:44:41.149 20391-20391/com.socialivemusic.socialivemusic W/System.err: at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
02-24 11:44:41.159 20391-20391/com.socialivemusic.socialivemusic W/System.err: at dalvik.system.NativeStart.main(Native Method)
任何人都瞭解,我發現了錯誤?我在登錄頁面上有非常類似的代碼,對我來說工作得很好。
某處你傳遞一個JSONArray,而不是一個JSONObject – JCoder
你能張貼JSON你得到的樣本?我想你會得到一個空的json數組,但是一個樣本可以幫助更好地查明問題。 –
此方法'GetDiscoverContent.fromJSON(JSONArray),其中'並不實際上要求JSONArray或JSONObject的? – JCoder