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

时间:2014-09-23 11:17:05

标签: android json

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

  

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();
        }

请帮忙

4 个答案:

答案 0 :(得分:2)

在执行JSON编码之前将BOM从您的String中剥离

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

答案 1 :(得分:1)

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

            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;
        }

答案 2 :(得分:1)

只需添加

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

在顶部

然后在结果

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

答案 3 :(得分:0)

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