gpt4 book ai didi

java - JSON Post 请求不包含参数

转载 作者:行者123 更新时间:2023-12-01 21:53:03 25 4
gpt4 key购买 nike

我的 json post 请求有问题。我创建了一个 JsonObject 并想将其发布到服务器,但服务器收到的发布请求的正文不包含任何内容,我不知道为什么......

public class ServiceHandler {
static String response = null;
public final static int GET = 1;
public final static int POST = 2;
String contentType = "application/json";
public ServiceHandler() {
}

public String makeServiceCall(String url, int method) {
return this.makeServiceCall(url, method, null);
}

public String makeServiceCall(String url, int method, List<NameValuePair> params) {
try {
// http client
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;

// Checking http request method type
if (method == POST) {
HttpPost httpPost = new HttpPost(url);
// adding post params
if (params != null) {
JSONObject jsonObj = new JSONObject();
jsonObj.put("name", "your name");
jsonObj.put("message", "your message");

StringEntity entity = new StringEntity(jsonObj.toString(), HTTP.UTF_8);
httpPost.setEntity(entity);
}

httpResponse = httpClient.execute(httpPost);
} else if (method == GET) {
// appending params to url
if (params != null) {
String paramString = URLEncodedUtils
.format(params, "utf-8");
url += "?" + paramString;
}

HttpGet httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);
}

httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}

return response;
}

最佳答案

提供“Content-Type” header 以请求值“application/json”。服务器似乎找不到正确的消息正文映射器。

关于java - JSON Post 请求不包含参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34864820/

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