从 HttpResponse 中提取消息正文
好的,我已成功连接到远程服务器并收到 HTTP/1.1 200 OK
响应,并且该响应被打包到 HttpResponse 对象中。现在我如何从中获取响应中的数据,特别是从服务器发送的 JSON?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
像这样的东西:这里重复: How do I parse JSON from Java HTTPResponse?
something like this: duplicate here : How do I parse JSON from a Java HTTPResponse?
那么,您可以通过调用返回
HttpEntity
类型的对象的getEntity()
来获取HttpResponse
的正文。然后,您将需要使用从HttpEntity
的getContent()
方法返回的InputStream
。我会这样做:Well, you can get the body of the
HttpResponse
by callinggetEntity()
which returns an object of typeHttpEntity
. You will then want to consume theInputStream
that is returned from thegetContent()
method of theHttpEntity
. I would do it like this:您还可以使用 EntityUtils
PS:您可能必须在主线程之外的单独线程中执行此操作,例如在 AsyncTask 的 doInBackground() 中,或者主线程上的网络操作可能会发生异常。
You may also use EntityUtils
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.
调用 httpclient.execute() 时使用 BasicResponseHandler
Use a BasicResponseHandler when calling httpclient.execute()