我目前正在开发一个需要发送发布请求并从服务器获取 json 对象的项目。之前我使用 Get 方法来访问 json 对象。它工作得很好。但是由于一些服务器更改,我不得不转向发布方法。然后它不会返回我之前从“get”方法获得的 json 对象。我尽力想出一个解决方案,但不能。如果有人能帮助我解决这个问题,我将不胜感激。
private AdSniperAdObjectResponse postData(String url) {
//Bundle b = new Bundle();
HttpClient httpClient = HttpClientFactory.getThreadSafeClient();
//Log.d(TAG, "url: " + url);
try {
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("Accept", "JSON");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("latitude", "-33.8736"));
nameValuePairs.add(new BasicNameValuePair("longitude", "151.207"));
nameValuePairs.add(new BasicNameValuePair("age", "35"));
nameValuePairs.add(new BasicNameValuePair("gender", "All"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity resEntity = httpResponse.getEntity();
if (resEntity != null) {
String resp = EntityUtils.toString(resEntity);
以上是我使用的代码。早些时候我使用了 HttpGet 类。对于 HttpPost,“resp”变量始终为空。不知道我做错了什么。