Reputation: 251
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
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
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