0

Could you help ? My PHP web service read send this JSON:

{
  "version": "1",
  "id": "1",
  "title": "John",
  "text": "Joe",
  "language": "French",
  "image": "ic_launcher1",
  "audio": "au_sound1",
}

My Android Rest client code return this following error :

{
  "version": "1",
  "id": "1",
  "title": "John",
  "text": "Joe",
  "language": "French",
  "image": "ic_launcher1",
  "audio": "au_sound1",
} 
of type java.lang.String cannot be converted to JSONObject at org.json.JSON.typeMismatch(JSON.java:111)

Android client code is below :

private void webServiceClient (String serviceUrl) {
        String jsonContent;
        InputStream inputStream = null;
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(serviceUrl);
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            inputStream = entity.getContent();
        }
        catch (Exception e) {
            Log.e("Webservice 1", e.toString());
        }


        try {
            // read the json file ----------------------------------------
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"), 8000);
            StringBuilder sb = new StringBuilder();
            String line;

            while ((line = reader.readLine()) != null) {

                sb.append(line + "\n");
            }

            reader.close();
            jsonContent = sb.toString();

            if (jsonContent != null) {
                JSONObject jsonObject = new JSONObject(jsonContent);
                if (jsonObject != null) {
                    JSONArray sectionArray = null;
                    String language = jsonObject.get("language").toString();

                    switch (language) {
                    case "French":
                        sectionArray = (JSONArray) jsonObject.get("section_fr");
                        break;
                    case "English":
                        sectionArray = (JSONArray) jsonObject.get("section_en");
                        break;
                    case "Arabic":
                        sectionArray = (JSONArray) jsonObject.get("section_ar");
                        break;
                    default:
                        break;
                    }

                    List<String> sections = new ArrayList<String>();
                    for (int i = 0; i < sectionArray.length(); i++) {
                        String oneSection = sectionArray.get(i).toString();
                        sections.add(oneSection);
                        Log.d(TAG, "section %%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
                                + oneSection);
                    }
                    /*
                     * jsonObject.get("id"); jsonObject.get("title");
                     * jsonObject.get("text"); jsonObject.get("language");
                     */
                    addSections(jsonObject.get("id").toString(), jsonObject
                            .get("language").toString(), sections);
                }
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

The JSON is seems to have something wrong I don't knowYour help bill be very appreciated.

3
  • Don't change the details in your question. It makes answers obsolete. If you need to, edit your question to add details. Commented Dec 1, 2014 at 16:20
  • Also, please post the full stack trace. Commented Dec 1, 2014 at 16:28
  • OK, this is my first question. I will follow your advises next time ... Commented Dec 1, 2014 at 16:31

1 Answer 1

1

the json is not valid. You have an extra comma in the end

 "audio": "au_sound1",
}

should be

 "audio": "au_sound1"
}

also chage

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

with

 jsonContent = EntityUtils.toString(entity)
Sign up to request clarification or add additional context in comments.

2 Comments

I'm still having the same issue after I remove the trailing comma. Thanks
Could you explain the need of this call : jsonContent = EntityUtils.toString(entity)

Your Answer

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