0

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
3
  • 3
    Log the response from the server before you try to create the JSONObject and post it here. Apparently you're not getting valid JSON back. Commented May 6, 2014 at 17:27
  • 1
    And please do not post questions twice! Commented May 6, 2014 at 17:27
  • Reference":"","Objet":{"Login":null,"Mtspass":null,"Code_session":"616e0e76-fc5e-4df7-9079-d65cc15f1dd7"},"Status_code":"","Status_label":""} Commented May 7, 2014 at 8:38

1 Answer 1

2

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"}]}

Sign up to request clarification or add additional context in comments.

5 Comments

no it's not like that this is the whole reponse from the server looks like:{Reference":"","Objet":{"Login":null,"Mtspass":null,"Code_session":"616e0e76-fc5e‌​-4df7-9079-d65cc15f1dd7"},"Status_code":"","Status_label":""}
do a System.out.println((new String(buffer)) and let me see the result
05-07 15:01:51.002: I/System.out(8550): "{\"Reference\":\"test\",\"Objet\":{\"Login\":null,\"Mtspass\":null,\"Code_session\":\"6916cd6a-6a0e-4ee4-a162-2c4e2e67244f\"},\"Status_code\":\"\",\"Status_label\":\"\"}"
here is the problem, normally, it has to be like that {"Reference":"test",...} it shouldn't have the backslash, I dont know where it comes from, but try to verify your web service
I found the problem, its the backslash and quotes this is my solution: String x=new String(buffer); String y=x.replace("\\",""); if (y.startsWith("\"")) { y = y.substring(1, y.length()); } if (y.endsWith("\"")) { y = y.substring(0, y.length() - 1); } JSONObject reader = new JSONObject(y);

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.