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();
    }       
}
10
  • 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. Commented Oct 16, 2015 at 13:20
  • What if you print value? Commented Oct 16, 2015 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? Commented Oct 16, 2015 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. Commented Oct 16, 2015 at 13:21
  • Possible duplicate of How can I get an http response body as a string in Java? Commented Oct 16, 2015 at 13:22

2 Answers 2

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.

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

1 Comment

Edited the question with modified code. Still same error using that code. Thanks for the immediate response. But can you help me further.
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();
}       
}

Comments

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.