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.

Community
  • 1
  • 1
Jeremy Piednoel
  • 407
  • 2
  • 5
  • 12

1 Answers1

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;

        }
Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37
  • i delete the \n but your trick with substring make an error : debug JSONException(4461): org.json.JSONException: Unterminated object at character 242 of – Jeremy Piednoel Jul 23 '13 at 08:24
  • Nice very good site i found the mistake it was a space between [" "] in my JSON, i update my question. TYVM ! – Jeremy Piednoel Jul 23 '13 at 08:36