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字符串。我如何从我的response
中获取?顺便说一句,我已经提供了GSON
库,我可以使用它的任何方法吗?从HttpResponse获取json
解决方案类似于这个看起来很丑陋,IMO:best way to handle json from httpresponse android
还有很多更好的办法,对不对?
任何帮助表示赞赏,感谢
更新:
String json = EntityUtils.toString(response.getEntity());
似乎这样的伎俩。只有一个小问题:字符串被包裹在括号内[]
。我应该手动删除它们吗?他们是由PHP生成:■json_encode()
方括号表示一个JSON数组。你应该像这样处理响应。 – Perception
@Perception正确。我从php代码中删除了我的容器数组,并解决了这个问题。谢谢 – Johan
@Johan你可以发布你的答案并接受它,以便其他人可能会发现它有用,并知道究竟是什么解决了问题。谢谢! –