0

尝试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)
4

3 回答 3

2

Ensure that the server is sending UTF-8 encoded data. Also see that there are no new line characters in the beginning/end.

Eclipse's logcat tool will help you find these special characters. Logcat on command line sometimes doesnt render the special characters.

于 2012-12-02T10:30:34.980 回答
0

My only though is that you are somehow destroying the string as it comes in with your httpget code, I would recommend you use a library for http requests such as EasyHttpClient , just create that class in your project and then instead of all that code you have do

EasyHttpClient client = new EasyHttpClient();
String jsonString = client.get("http://10.0.2.2:63200/OnlineArabic/api/datasen");

Then see if you can put that in an object. I cant test that code unfortunately I only have android 4.2 installed here.

于 2012-12-02T08:05:35.097 回答
0

In your code,

BufferedReader reader = new BufferedReader(new InputStreamReaderinstream , "UTF-8"),8 );

change this to

BufferedReader reader = new BufferedReader(new InputStreamReader(instream , "UTF-8"),8 );

于 2012-12-02T10:58:06.303 回答