Abdellah Felahi
Abdellah Felahi

Reputation: 23

Java.lang.String cannot be converted to jsonobeject

my problem is that when i try to parse from my wcf service into android it gives me an error. the return for my service is with JSON. this is my android code:

  DefaultHttpClient client = new DefaultHttpClient();
    // http get request
    HttpGet request = new HttpGet("http://XXX.XXX.XXX.XXX:8181/Managers/Authentification.svc/authen/?loga="+t1.getText().toString()+"&pass="+t2.getText().toString());

    // set the hedear to get the data in JSON formate
    request.setHeader("Accept", "application/json");
    request.setHeader("Content-type", "application/json");

    //get the response
    HttpResponse response = client.execute(request);
    System.out.println("Connectee--->" +response.getAllHeaders());

    HttpEntity entity = response.getEntity();

    //if entity contect lenght 0, means no data exist in the system with these code
    if(entity.getContentLength() != 0) {
        // stream reader object
        Reader employeeReader = new InputStreamReader(response.getEntity().getContent());
        //create a buffer to fill it from reader
        char[] buffer = new char[(int) response.getEntity().getContentLength()];
        //fill the buffer by the help of reader
        employeeReader.read(buffer);
        //close the reader streams
        employeeReader.close();

        //for the employee json object
        JSONObject tt =  new JSONObject(new String(buffer));

        System.out.println("..................OK.........................");

        System.out.println("Reference: " + tt.getString("Reference"));



    }
    else {
        System.out.println("...................Not OK........................");
    }

}
catch (JSONException e) {
    System.out.println("...................ERRORJSON........................");
    e.printStackTrace();
} catch (Exception e) {
    // TODO Auto-generated catch block
    System.out.println("...................ERROR........................");
    e.printStackTrace();
}

}
});

error message :

05-06 16:34:04.256: I/System.out(7257): Connectee--->[Lorg.apache.http.Header;@40d2b900
05-06 16:34:04.296: I/System.out(7257): ...................ERRORJSON........................
05-06 16:34:04.316: W/System.err(7257): org.json.JSONException: Value {"Reference":""} of type java.lang.String cannot be converted to JSONArray

Upvotes: 0

Views: 144

Answers (1)

El Arabi
El Arabi

Reputation: 103

u have an array attribute : Reference, which has to be defined in JSONString like this: {"Reference":[]} for example {"Reference":[{"id":1,"referenceName":"name1"},{"id":2,"referenceName":"name2"}]}

Upvotes: 2

Related Questions