从Htt的presponse得到JSONHtt、presponse、JSON

2023-09-05 08:39:15 作者:搞笑先生

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的丑陋,海事组织: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.