Okay, I've successfully connected to a remote server and received a HTTP/1.1 200 OK
response and the response is packed into the HttpResponse object. Now how do I get the data in the response out of it, specifically the JSON that was sent from the server?
好的,我已成功连接到远程服务器并收到HTTP / 1.1 200 OK响应,并且响应被打包到HttpResponse对象中。现在我如何获取响应中的数据,特别是从服务器发送的JSON?
4 个解决方案
#1
24
something like this: duplicate here : How do I parse JSON from a Java HTTPResponse?
像这样的东西:复制在这里:我如何解析Java HTTPResponse中的JSON?
HttpResponse response; // some response object
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String json = reader.readLine();
JSONTokener tokener = new JSONTokener(json);
JSONArray finalResult = new JSONArray(tokener);
#2
4
Well, you can get the body of the HttpResponse
by calling getEntity()
which returns an object of type HttpEntity
. You will then want to consume the InputStream
that is returned from the getContent()
method of the HttpEntity
. I would do it like this:
好吧,你可以通过调用getEntity()来获取HttpResponse的主体,它返回一个HttpEntity类型的对象。然后,您将需要使用从HttpEntity的getContent()方法返回的InputStream。我会这样做:
public static String entityToString(HttpEntity entity) {
InputStream is = entity.getContent();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
StringBuilder str = new StringBuilder();
String line = null;
try {
while ((line = bufferedReader.readLine()) != null) {
str.append(line + "\n");
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
is.close();
} catch (IOException e) {
//tough luck...
}
}
return str.toString();
}
#3
1
You may also use EntityUtils
您也可以使用EntityUtils
response = cl.execute(p); //cl is http client and p is the post request
if(response.getStatusLine().getStatusCode()==200)
{
try
{
String resp_body = EntityUtils.toString(response.getEntity());
Log.v("resp_body", resp_body.toString());
JSONObject jsobj = new JSONObject(resp_body);
}
catch(Exception e)
{
Log.e("sometag",e.getMessage());
}
}
PS : You may have to do this in a separate thread, other than the main thread, like in the doInBackground() of an AsyncTask or Network operation on main thread exception may occur.
PS:您可能必须在除主线程之外的单独线程中执行此操作,例如在AsyncTask的doInBackground()中,或者可能发生主线程异常上的网络操作。
#4
0
Use a BasicResponseHandler when calling httpclient.execute()
调用httpclient.execute()时使用BasicResponseHandler
ResponseHandler <String> resonseHandler = new BasicResponseHandler();
String response = httpclient.execute(httpget, resonseHandler);
#1
24
something like this: duplicate here : How do I parse JSON from a Java HTTPResponse?
像这样的东西:复制在这里:我如何解析Java HTTPResponse中的JSON?
HttpResponse response; // some response object
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String json = reader.readLine();
JSONTokener tokener = new JSONTokener(json);
JSONArray finalResult = new JSONArray(tokener);
#2
4
Well, you can get the body of the HttpResponse
by calling getEntity()
which returns an object of type HttpEntity
. You will then want to consume the InputStream
that is returned from the getContent()
method of the HttpEntity
. I would do it like this:
好吧,你可以通过调用getEntity()来获取HttpResponse的主体,它返回一个HttpEntity类型的对象。然后,您将需要使用从HttpEntity的getContent()方法返回的InputStream。我会这样做:
public static String entityToString(HttpEntity entity) {
InputStream is = entity.getContent();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
StringBuilder str = new StringBuilder();
String line = null;
try {
while ((line = bufferedReader.readLine()) != null) {
str.append(line + "\n");
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
is.close();
} catch (IOException e) {
//tough luck...
}
}
return str.toString();
}
#3
1
You may also use EntityUtils
您也可以使用EntityUtils
response = cl.execute(p); //cl is http client and p is the post request
if(response.getStatusLine().getStatusCode()==200)
{
try
{
String resp_body = EntityUtils.toString(response.getEntity());
Log.v("resp_body", resp_body.toString());
JSONObject jsobj = new JSONObject(resp_body);
}
catch(Exception e)
{
Log.e("sometag",e.getMessage());
}
}
PS : You may have to do this in a separate thread, other than the main thread, like in the doInBackground() of an AsyncTask or Network operation on main thread exception may occur.
PS:您可能必须在除主线程之外的单独线程中执行此操作,例如在AsyncTask的doInBackground()中,或者可能发生主线程异常上的网络操作。
#4
0
Use a BasicResponseHandler when calling httpclient.execute()
调用httpclient.execute()时使用BasicResponseHandler
ResponseHandler <String> resonseHandler = new BasicResponseHandler();
String response = httpclient.execute(httpget, resonseHandler);