user4312183
user4312183

Reputation:

type java.lang.String cannot be converted to JSONObject at org.json.JSON.typeMismatch(JSON.java:111)

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.

Upvotes: 0

Views: 614

Answers (1)

Blackbelt
Blackbelt

Reputation: 157437

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)

Upvotes: 1

Related Questions