从Htt的presponse得到JSON [英] get json from HttpResponse

查看:327
本文介绍了从Htt的presponse得到JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://my.server:8080/android/service.php");


List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("action", "getjson"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

HttpResponse response = httpclient.execute(httppost);

service.php 生成一个JSON字符串。如何将我从我的响应获取它?顺便说一句,我icluded的 GSON 库,我可以使用任何方法,这也许是?

service.php generates a json string. How would i fetch it from my response? Btw; I've icluded the GSON library, can i make use of any methods in it perhaps?

解决方案类似,这个看起来pretty的丑陋,海事组织:<一href="http://stackoverflow.com/questions/11081275/best-way-to-handle-json-from-htt$p$psponse-android">best方法来处理JSON从HTT presponse安卓

Solutions similar to this one looks pretty ugly, imo: best way to handle json from httpresponse android

还有很多更好的办法,对吧?

There much be better ways, right?

任何帮助是AP preciated,谢谢

Any help is appreciated, thanks

更新:

String json = EntityUtils.toString(response.getEntity());

似乎这样的伎俩。只有一个小问题:该字符串是包裹着括号 [] 。我应该手动删除它们?他们是由PHP生成:■ json_en code()

seems to do the trick. There is just one small issue: The string is wrapped with brackets []. Should i remove them manually? They are generated by php:s json_encode()

推荐答案

这个问题是在我的PHP文件。从JSON EN codeD对象取出容器阵列做我的Java code的工作。

The problem was in my php file. Removing the container array from the json encoded object made my java code work.

这篇关于从Htt的presponse得到JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆