0
       01-16 08:16:19.210: W/System.err(1394): org.json.JSONException: Value 

    {"GetDoctorsListResult":[
    {"Name":"Sujay Sharma","DoctorID":154,"Clinics":null,"speciality":"Allergy and immunology","Locality":"Mumbai","Address":"Abcc Building  "}
        ,{"Name":"Samir Tapiawala","DoctorID":159,"Clinics":null,"speciality":"Homeopath","Locality":"Mumbai","Address":"57\/101, Jawahar Nagar RD. NO. 6, GOREGAON WEST "}
        ,{"Name":"Sahil Kuma","DoctorID":171,"Clinics":null,"speciality":"Aerospace Medicine","Locality":"Mumbai","Address":"Digvija  "}
        ,{"Name":"Himesh Jagtap","DoctorID":180,"Clinics":null,"speciality":"Bariatric Surgery","Locality":"Mumbai","Address":"Abc- Society  "}
        ,{"Name":"Aarnav Prabhudesai","DoctorID":190,"Clinics":null,"speciality":"Dentistry","Locality":"Mumbai","Address":"Joyvilla Jawahar Nagar  "}
        ,{"Name":"Neeharika Saxana","DoctorID":197,"Clinics":null,"speciality":"Gynaecologist","Locality":"Mumbai","Address":"Joyvilla, Jawahar Nagar  "}
        ,{"Name":"Neeharika Saxena","DoctorID":205,"Clinics":null,"speciality":"Gynaecologist","Locality":"Mumbai","Address":"Joyvilla  "}
        ,{"Name":"Ravi Sharma","DoctorID":207,"Clinics":null,"speciality":"Ayurvedacharya","Locality":"Mumbai","Address":"Q\/02  "}
        ,{"Name":"Prashant Bhatt","DoctorID":209,"Clinics":null,"speciality":"Dentistry","Locality":"Mumbai","Address":"202 Anand Vihar , Bldg No-2 , D-wing , Bhawani Chowk , b--cabin Road , Ambernath(e). Bhawani Chowk"}
        ,{"Name":"Samidha Sen","DoctorID":210,"Clinics":null,"speciality":"Addiction Medicine","Locality":"Mumbai","Address":"S\/09  "}
        ,{"Name":"Subodh Mehta","DoctorID":212,"Clinics":null,"speciality":"Orthopaedic Surgery","Locality":"Mumbai","Address":"Opera  "}]} of type org.json.JSONObject cannot be converted to JSONArray

here is my activity code:

  class DownloadTask extends AsyncTask<String, Void, Object> {
    protected Boolean doInBackground(String... params) {
        try {
            Thread.sleep(4000); // Do your real work here
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return true; // Return your real result here
    }

    protected void onPostExecute(Object result) {
        // Pass the result data back to the main activity
        DoctorSearchActivity.this.data = result;
        if (DoctorSearchActivity.this.progressdialog != null) {
            DoctorSearchActivity.this.progressdialog.dismiss();
        }
        try {
            JSONStringer geneology = new JSONStringer()


                    .object().key("Params").object().key("TypesOnSearch")
                    .value("name").key("CharactersToSearch")
                    .value("s")
                    .endObject();

            JSONArray results = new JSONArray();
            results = BC
                    .returnJSONArray(geneology,
                            "http://192.168.2.27/HYEHR_WCFService/DoctorService.svc/GetDoctorsList");
            if (results.length() == 0) {
                Toast.makeText(
                        DoctorSearchActivity.this,
                        "Error Occured while loading data.Try again later.",
                        Toast.LENGTH_LONG).show();
            }
            // for (int i = 0; i < results.length(); i++) {
            // Log.v("data", results.getString(i));
            // }
            // aTable = BC.appendRows(aTable, results,
            // DoctorSearchActivity.this);
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

}

and my json function : public JSONArray returnJSONArray(JSONStringer JsonString, String url) {

    results = new JSONArray();

    try {
        HttpPost request = new HttpPost(url);
        request.setHeader("Accept", "application/json");
        request.setHeader("Content-type", "application/json");

        // Build JSON string
        StringEntity entity = new StringEntity(JsonString.toString());
        request.setEntity(entity);
        Log.v("data", request + JsonString.toString());
        // Log.v("data sender",request.setEntity(entity));
        // Send request to WCF service
        DefaultHttpClient httpClient1 = new DefaultHttpClient();
        HttpResponse response = httpClient1.execute(request);
        Log.v("response code", response.getStatusLine().getStatusCode()
                + "");
        HttpEntity responseEntity = response.getEntity();
        // Read response data into buffer
        char[] buffer = new char[(int) responseEntity.getContentLength()];
        InputStream stream = responseEntity.getContent();
        InputStreamReader reader = new InputStreamReader(stream);
        reader.read(buffer);
        stream.close();
        results = new JSONArray(new String(buffer));
        Log.v("results length : ", results.length() + "");
    }
         catch (Exception e) {
        // i mean sending data without key
        // TODO: handle exception
        e.printStackTrace();
    }
    return results;
}

I am getting this error i am passing data in jsonobject and receiving data in jsonarray.

vipul mittal
  • 17,343
  • 3
  • 41
  • 44
Kirti
  • 691
  • 8
  • 27

4 Answers4

3

Try to replace this:

results = new JSONArray(new String(buffer));

by this:

results = new JSONObject(new String(buffer)).getJSONArray("GetDoctorsListResult");
Damien R.
  • 3,383
  • 1
  • 21
  • 32
2

The root element of the JSON from URL is a JSONObject you should first parse it to JSONObject and then get GetDoctorsListResult which is an array from the object.

vipul mittal
  • 17,343
  • 3
  • 41
  • 44
1
HttpEntity entity = httpResponse.getEntity(); 
String result =  EntityUtils.toString(entity);
JSONObject jsonObject=new JSONObject(result);
JSONArray jsonArray=jsonObject.getJSONArray("GetDoctorsListResult");

use this code after getting response.

SirDarius
  • 41,440
  • 8
  • 86
  • 100
PankajSharma
  • 1,529
  • 14
  • 27
1

I think your First Element of object GetDoctorsListResult is JSON array . So you have to take its value in json array.

REFER THIS THREAD and THIS ONE

JSONArray DoctorsList= jsonResponse.getJSONArray("GetDoctorsListResult");

Community
  • 1
  • 1
Straw Hat
  • 902
  • 14
  • 38