【问题标题】:org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONArrayorg.json.JSONException:java.lang.String 类型的值 无法转换为 JSONArray
【发布时间】:2014-09-23 11:17:04
【问题描述】:

我使用上述服务从服务器获取响应并将其解码为字符串生成器。但是当我运行该应用程序时,它会显示警告

org.json.JSONException: java.lang.String 类型的值 无法转换为 JSONArray

在日志猫中。

服务器端没有问题。

             protected String doInBackground(String... params) {


            StringBuilder stringBuilder = new StringBuilder();
       try{
            HttpResponse response = null;
            HttpParams httpParameters = new BasicHttpParams();
            HttpClient client = new DefaultHttpClient(httpParameters);

            JSONObject jobj = new JSONObject();
            try {

                jobj.put("page_id",pageID);


            } catch (JSONException e) {
                e.printStackTrace();
            }

            String url = "http://myservice.com/gustbook_berry/mobile/GetOrder";
            Log.i("Send URL:", url);
            // HttpGet request = new HttpGet(url);
            HttpPost request = new HttpPost(url);

            List<NameValuePair> page = new ArrayList<NameValuePair>(1);
            page.add(new BasicNameValuePair("page_id", jobj.toString()));

            Log.d(TAG, url + page);
            request.setEntity(new UrlEncodedFormEntity(page));

            response = client.execute(request);

            HttpEntity entity = response.getEntity();

            InputStream stream = entity.getContent();
            //String output=EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
            //System.out.println("OUT PUT->>"+output);
            int b;
            while ((b = stream.read()) != -1) {
                stringBuilder.append((char) b);

                System.out.println(stringBuilder.toString()+"\n");
            }
       }catch(Exception e){
           System.out.println(" error ocurres :" + e.getMessage());
       }

       return stringBuilder.toString();
        }

请帮忙

【问题讨论】:

  • 记录响应并检查它是否使用任何字符集或算法进行编码
  • 没有它没有编码。它的痛苦 json 文本
  • BOM,BOM,BOM,BOM ...
  • 确保您的字符串是 JSON 文本...
  • 那些字符是 UTF-8 BOM。请参阅stackoverflow.com/questions/4614378/… 了解如何禁用它

标签: android json


【解决方案1】:

在进行 JSON 编码之前从字符串中去除 BOM

if (yourstring.startsWith("\ufeff")) {
    yourstring = yourstring.substring(1);
}

【讨论】:

  • 我解决了:if (yourstring.startsWith("")) { yourstring = yourstring.substring(3); }
【解决方案2】:

感谢您的回答。我发现我错了。所以我改变了代码。希望这段代码对某人有所帮助..

            protected JSONArray doInBackground(String... params) {


            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://myservice.com/gustbook_berry/mobile/GetOrder");

            HttpResponse response = null;
            StringBuilder result = new StringBuilder();

            try {

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);

                nameValuePairs.add(new BasicNameValuePair("page_id",getPageID()));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));


                // Execute HTTP Post Request
                response = httpclient.execute(httppost);

            } catch (Exception e) {
                // TODO: handle exception
            }


            try {

                HttpEntity entity = response.getEntity();

                InputStream stream = entity.getContent();
                int b;
                while ((b = stream.read()) != -1) {
                    result.append((char) b);
                }

            } catch (Exception e) {
                // TODO: handle exception
            }


            JSONArray arrayResult = null;



            try {
                arrayResult = new JSONArray(result.toString());

            } catch (JSONException e) {

                e.printStackTrace();
                try {
                    arrayResult = new JSONArray(result.substring(3));
                } catch (JSONException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                    arrayResult = new JSONArray();
                }
            }


       return arrayResult;
        }

【讨论】:

    【解决方案3】:

    只需添加

    header('Content-Type: application/json; charset=utf-8'); 
    

    置顶

    然后在结果中

    打印(json_encode($resArr,JSON_UNESCAPED_UNICODE));

    【讨论】:

      【解决方案4】:

      将编码更改为-> UTF-8 代码页 65001

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-26
        • 2015-11-05
        • 1970-01-01
        • 1970-01-01
        • 2012-05-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多