0

Im using above service to get response from the server and decode it to a string builder. But when i run the app it shows a warning

org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONArray

in the log cat.

There is no problam with the server side.

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

Please help

san88
  • 1,286
  • 5
  • 35
  • 60

4 Answers4

2

Strip the BOM out of your String before doing the JSON encode

if (yourstring.startsWith("\ufeff")) {
    yourstring = yourstring.substring(1);
}
isalgueiro
  • 1,973
  • 16
  • 20
  • 1
    I solved with: `if (yourstring.startsWith("")) { yourstring = yourstring.substring(3); }` – Eren Feb 19 '16 at 19:37
1

Thank for your answers. I found where im wrong.So i have changed the code. hope this code will help to someone..

            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;
        }
san88
  • 1,286
  • 5
  • 35
  • 60
1

simply add

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

on top

and then in result

print(json_encode($resArr,JSON_UNESCAPED_UNICODE));

imabhishek
  • 61
  • 1
  • 7
0

Change Your Encoding To-> UTF-8 Codepage 65001

Abbas m
  • 56
  • 3