gpt4 book ai didi

android - 为什么我的 HttpPut 从我的 Android 发送 GET 请求?

转载 作者:行者123 更新时间:2023-11-29 14:08:39 25 4
gpt4 key购买 nike

我尝试从我的 Android 设备连接到 REST 网络服务并在那里放置一些数据,但我得到的只是一个 GET 结果。甚至我的网络服务器也坚持认为这是一个 GET 请求。我的代码有什么问题?

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPut httpPut = new HttpPut(url);
httpPut.addHeader("Content-Type", "application/xml; charset=utf-8");
httpPut.addHeader("User-Agent", "Android");
HttpEntity entity = new StringEntity(data);
httpPut.setEntity(entity);
HttpResponse response = httpclient.execute(httpPut);
HttpEntity input = response.getEntity();
StringBuilder result = new StringBuilder();
if (input != null) {
InputStream instream = input.getContent();
result = convertStreamToString(instream);
instream.close();
}
httpclient.getConnectionManager().shutdown();
return result.toString();

最佳答案

使用 JSONStringer 这可能会解决您的问题...

我为此挣扎了很多............

你的代码应该是这样的......

HttpPut request = new HttpPut("your URL");
JSONStringer json = new JSONStringer()
.object()
.key("cname").value(name)
.key("cmail").value(email)

.endObject();

StringEntity entity = new StringEntity(json.toString());
entity.setContentType("application/json;charset=UTF-8");//text/plain;charset=UTF-8
entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
request.setEntity(entity);
// Send request to WCF service
DefaultHttpClient httpClient = new DefaultHttpClient();

HttpResponse response = httpClient.execute(request);

谢谢...

关于android - 为什么我的 HttpPut 从我的 Android 发送 GET 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5077092/

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