尝试JSONObject
在 android中创建时出现此错误
org.json.JSONException: Value {"0" : [{"original" : "car", "translation" : "araba"}]}
类型java.lang.String
不能转换为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)