user972590
user972590

Reputation: 251

How to read the response from the HTTPClient

I am doing work around HTTPClient to send the POST request and get the response. I am getting the response code is OK.

I want to read few contents from that response HttpEntity entity =response.getEntity(); I am printing that response on console System.out.println(EntityUtils.toString(entity));.

it's printing {"created_at":"2011-12-01T07:56:50+00:00","type":"sample","id":29,"status":"received"}

I want to read the id from the above string,.

Any one can help how to retrieve the id from the entity.

Thanks in advance..

Upvotes: 1

Views: 4345

Answers (3)

erimerturk
erimerturk

Reputation: 4288

this way is not very efficent but you can try it also.

BufferedReader  br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
        String readLine;
        while(((readLine = br.readLine()) != null)) {
          System.err.println(readLine);
      }

if(readLine.indexOf(",") > -1 && readLine.indexOf(":") > -1)
String id = (readLine.split(",")[2]).split(":")[1];

Upvotes: 0

Buhake Sindi
Buhake Sindi

Reputation: 89189

The data is JSON data.

You can go to json.org and download JSONObject.

You can do this,

JSONObject json = new JSONObject(EntityUtils.toString(entity));
int id = json.getInt("id");

Upvotes: 3

Giovanni
Giovanni

Reputation: 4015

It seems that you response is JSON one. can you use a JSON library, Jackson for example?

Upvotes: 0

Related Questions