3

Hi i got a problem with Android JSON Parser in 2.3, all is ok in 4.0>.

I look other topic they are talking about encoding (server) or other stuff server side, but i tried to put "test" in all my JSON field an the problem still persist.

Here is my code :

URL url = new URL(c.getString(R.string.url_ws) + url_ws);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestMethod("POST");
OutputStreamWriter request = new OutputStreamWriter(connection.getOutputStream());
request.write("&test=test");
request.flush();
request.close();


String line;
InputStreamReader isr = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();

while ((line = reader.readLine()) != null){
    sb.append(line + "\n");
}
String json = sb.toString().trim();
try {
     JSONObject obj = new JSONObject(json);
} catch (JSONException e) {
    e.printStackTrace();
    Log.d(Tools.TAG+"/debug JSONException", e.toString());
    return null;
}

Here is my exception

debug JSONException(4184): org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONObject

i saw double space between "Value" and "of" so i guess the problem is a empty string but i dont get it.

thanks.

EDIT

it was a encode mistake in my JSON,

char a = json.substring(0, 1).charAt(0);
int ascii = (int)a;
Tools.myLog(">"+ascii+"<"); 

i found 65279 char

SOLUTION

Why is &#65279; appearing in my HTML?

encoding UTF-8 without BOM.

8
  • stackoverflow.com/questions/17792779/…. similar!. Post your json. Commented Jul 23, 2013 at 8:08
  • JSONArray obj = new JSONArray(json); use this instead of JSONObject obj = new JSONObject(json); Commented Jul 23, 2013 at 8:11
  • my JSON isn't an array : {"bHasError":false,"sSessionId":"5e10be14c408732179aa4731899882fd","iMembreId":7510064,"iCurrentDate":1374565720 .... Commented Jul 23, 2013 at 8:13
  • you need to show the JSON response Commented Jul 23, 2013 at 8:14
  • Log the string before creating the JSONObject so you can see if it's a json string. Commented Jul 23, 2013 at 8:18

1 Answer 1

2
while ((line = reader.readLine()) != null){
    sb.append(line + "\n");
}

instead try

while ((line = reader.readLine()) != null){
    sb.append(line);
   json = sb.toString().substring(0, sb.toString().length()-1);
}

Follow this method for getting json object:

 public JSONObject getJSONObjFromUrl(String url, List<NameValuePair> params) {      
            System.out.println("url:: "+url );
            System.out.println("params:: "+ params +" " +params.get(0) );
            // Making HTTP request
            try {
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        is, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }
                is.close();

                json = sb.toString().substring(0, sb.toString().length()-1);
               // Log.e("JSON:: ", json);
            } catch (Exception e) {
                Log.e("Buffer Error", "Error converting result " + e.toString());
            }

            // try parse the string to a JSON object
            try {
                jObj = new JSONObject(json);
            } catch (JSONException e) {
                Log.e("JSON Parser", "Error parsing data " + e.toString());
            }

            // return JSON String

            return jObj;

        }
Sign up to request clarification or add additional context in comments.

2 Comments

i delete the \n but your trick with substring make an error : debug JSONException(4461): org.json.JSONException: Unterminated object at character 242 of
Nice very good site i found the mistake it was a space between [" "] in my JSON, i update my question. TYVM !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.