我得到這個錯誤,而試圖創建一個JSONObject
定義字符串的Android無法解析從服務器檢索的JSON字符串中的Android,但可以解析爲Android的
java.lang.String
類型的org.json.JSONException: Value {"0" : [{"original" : "car", "translation" : "araba"}]}
不能轉換爲JSONObject
如果我使用從服務器使用HttpClient
返回的字符串,我會得到上述錯誤。
如果我把上面的同一個字符串,並定義一個靜態字符串,並解析它很好。當使用HttpClient
來檢索JSON
時,是否有一些解碼或編碼問題需要解決?
我手動建立JSON
字符串在C#作爲一個測試,在這裏它返回
return "{\"0\" : [{\"original\" : \"car\", \"translation\" : \"araba\"}]}";
是代碼
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
String s = "http://10.0.2.2:63200/OnlineArabic/api/datasen";
HttpGet httpGet = new HttpGet(s);
HttpResponse response = httpClient.execute(httpGet, localContext);
HttpEntity entity = response.getEntity();
InputStream instream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReaderinstream , "UTF-8"),8);
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
String result= convertStreamToString(instream);
String jsonString = sb.toString();
JSONObject jObject = new JSONObject(jsonString);
任何想法?
這裏是日誌:
12-01 13:48:08.156: W/System.err(22947): org.json.JSONException: Value {"0" : [{"original" : "car", "translation" : "araba"}]} of type java.lang.String cannot be converted to JSONObject
12-01 13:48:08.160: W/System.err(22947): at org.json.JSON.typeMismatch(JSON.java:107)
12-01 13:48:08.160: W/System.err(22947): at org.json.JSONObject.<init>(JSONObject.java:158)
12-01 13:48:08.160: W/System.err(22947): at org.json.JSONObject.<init>(JSONObject.java:171)
12-01 13:48:08.170: W/System.err(22947): at com.ls.audioplayer.MainActivity.call(MainActivity.java:137)
12-01 13:48:08.170: W/System.err(22947): at com.ls.audioplayer.MainActivity$1.onClick(MainActivity.java:294)
12-01 13:48:08.180: W/System.err(22947): at android.view.View.performClick(View.java:2408)
12-01 13:48:08.180: W/System.err(22947): at android.view.View$PerformClick.run(View.java:8816)
12-01 13:48:08.190: W/System.err(22947): at android.os.Handler.handleCallback(Handler.java:587)
12-01 13:48:08.190: W/System.err(22947): at android.os.Handler.dispatchMessage(Handler.java:92)
12-01 13:48:08.221: W/System.err(22947): at android.os.Looper.loop(Looper.java:123)
12-01 13:48:08.221: W/System.err(22947): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-01 13:48:08.221: W/System.err(22947): at java.lang.reflect.Method.invokeNative(Native Method)
12-01 13:48:08.231: W/System.err(22947): at java.lang.reflect.Method.invoke(Method.java:521)
12-01 13:48:08.231: W/System.err(22947): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-01 13:48:08.240: W/System.err(22947): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-01 13:48:08.240: W/System.err(22947): at dalvik.system.NativeStart.main(Native Method)
1.日誌請 2.嘗試一個在線json格式器/轉換器來檢查你的json – pvllnspk
我已經添加了日誌 – stack
你的Android代碼在哪裏創建JSONObject? – mbwasi