gpt4 book ai didi

java - HttpPost 响应不返回 json 对象

转载 作者:行者123 更新时间:2023-11-30 11:45:32 24 4
gpt4 key购买 nike

我目前正在做一个项目,它需要发送一个 post 请求并从服务器获取一个 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”变量始终为空。不知道我做错了什么。

最佳答案

这不应该是这样吗

HttpResponse httpResponse = httpClient.execute(httpPost);

if (httpResponse != null) {
String resp = httpResponse.toString();

如果服务器返回 JSONString..

say JSONObject data = new JSONObject(resp);

然后获取值..

关于java - HttpPost 响应不返回 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10310751/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com