0

I tried searching for this error. There are many results on google for this search but nothing proved useful to me. This is my web service method

@GET
@Path("/values")
public String test() {

    return "{\"x\":5,\"y\":6}";

}

This is my client code

public class Check {
public static void main(String[] args){
    String url = "http://localhost:8181/math/webapi/values";
    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpGet request = new HttpGet(url);
    try {
        HttpResponse response = httpClient.execute(request);
        String value = response.toString();
        JSONObject json = new JSONObject(value);
        int i = json.getInt("x");
        System.out.println(i);
    }catch (Exception e) {
        e.printStackTrace();
    }       
}

The above code is a starter code and it is for learning how to use it. If this is solved, I have to apply the knowledge in another application. The client side code, I want to use the logic in android.

EDIT

public class Check {
public static void main(String[] args){
    String url = "http://localhost:8181/math/webapi/values";
    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpGet request = new HttpGet(url);
    try {
        HttpResponse response = httpClient.execute(request);
        InputStream value = response.getEntity().getContent();
        BufferedReader br = new BufferedReader(new InputStreamReader(value));
        String jsonValue = br.readLine();
        JSONObject json = new JSONObject(jsonValue);
        int i = json.getInt("x");
        System.out.println(i);
    }catch (Exception e) {
        e.printStackTrace();
    }       
}
Krishna
  • 601
  • 2
  • 9
  • 32
  • Likely your response contains a header? Try printing the value of `value`. **Edit** likely `response.toString` is not overridden from `Object#toString` and you're getting the `ClassName@hashCode` idiom. – Mena Oct 16 '15 at 13:20
  • What if you print `value`? – Thomas Weller Oct 16 '15 at 13:20
  • Are you sure the HTTP API you're using is synchronous? Are you sure that `HttpResponse#toString` returns the response rather than the generic `toString`? – T.J. Crowder Oct 16 '15 at 13:20
  • Step through the code in a debugger and examine `value`. That's the only rational way to approach figuring this out. If you don't have a debugger, stop what you're doing and get one. – T.J. Crowder Oct 16 '15 at 13:21
  • Possible duplicate of [How can I get an http response body as a string in Java?](http://stackoverflow.com/questions/5769717/how-can-i-get-an-http-response-body-as-a-string-in-java) – Thomas Weller Oct 16 '15 at 13:22
  • You need to obtain the content of the response and feed it into the JSON parser: http://stackoverflow.com/a/10804506/3215527 – wero Oct 16 '15 at 13:22
  • @T.J.Crowder thanks thomas for the suggestion. I'll debug it. – Krishna Oct 16 '15 at 13:25
  • @Thomas as I said, it is not the real application. I'm doing it to learn. I have to apply it in another application. – Krishna Oct 16 '15 at 13:26
  • @Mena Thanks for the help offered Mena. I'm not getting how can I override toString() using that response in client program. And what you said is right. I got the hashcode value when I did this `System.out.println(response);`. Can you help me further? – Krishna Oct 16 '15 at 13:39
  • @Krishna you may want to take a look at the API you're using (Apache?) and get a tutorial on how to parse the actualn response text, excluding headers. Clearly invoking `toString` won't work there. Overriding `toString` is likely not an option for you as this is not code you're owning. – Mena Oct 16 '15 at 13:55

2 Answers2

3

Fairly certain response.toString doesn't do what you think it does, as it's not listed in the documentation.

I believe you need to use response.getEntity, and then entity.getContent, which gives you an InputStream to read the content from. Then pass that stream into your parser.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Edited the question with modified code. Still same error using that code. Thanks for the immediate response. But can you help me further. – Krishna Oct 16 '15 at 13:37
1

Try this code. Use IOUtils as mentioned. it will work.

public class Check {
public static void main(String[] args){
String url = "http://localhost:8181/math/webapi/values";
HttpClient httpClient = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
try {
    HttpResponse response = httpClient.execute(request);
    InputStream value = response.getEntity().getContent();
    String jsonValue = IOUtils.toString(value);
    JSONObject json = new JSONObject(jsonValue);
    int i = json.getInt("x");
    System.out.println(i);
}catch (Exception e) {
    e.printStackTrace();
}       
}
Venkata
  • 135
  • 8