gpt4 book ai didi

android - 在 android 中使用 HttpPost 发布 JsonArray 和字符串参数

转载 作者:搜寻专家 更新时间:2023-11-01 08:59:09 24 4
gpt4 key购买 nike

我尝试了很多使用 HttpPost 发送数据的方法,但我没有成功,如果有人知道这个请帮忙?

我的服务网址是:
http://xxxxx/xxx/abc.php?id=xx&name=xxx&data=[{.....}]

当我从浏览器中点击时它工作正常但从我的代码 data=[{}] 没有发送。

我最后的尝试是:
1--->

String serviceUrl = "http://xxxxx/xxx/abc.php"; 


DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(serviceUrl);
httpPost.setHeader("id", "xx");
httpPost.setHeader("name", "xxx");
httpPost.setHeader("Content-Type", "application/json");
StringEntity entity = new StringEntity(jsonArr.toString(), HTTP.UTF_8);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
json_str = EntityUtils.toString(httpEntity);

2--->

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id", "xx") );
nameValuePairs.add(new BasicNameValuePair("name", "xxx"));
nameValuePairs.add(new BasicNameValuePair("data", jsonArr.toString()));

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(serviceUrl);
httpPost.setHeader("Content-Type", "application/json");
StringEntity entity = new StringEntity(nameValuePairs.toString(), HTTP.UTF_8);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
json_str = EntityUtils.toString(httpEntity);

3--->

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id", "xx") );
nameValuePairs.add(new BasicNameValuePair("name", "xxx"));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(serviceUrl);
httpPost.setHeader("Content-Type", "application/json");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
StringEntity entity = new StringEntity(jsonArr.toString(), HTTP.UTF_8);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
json_str = EntityUtils.toString(httpEntity);

输出:得到的响应与我在浏览器上使用 url 'http://xxxxx/xxx/abc.php?id=xx&name=xxx'(没有数据参数)得到的响应相同,我认为这意味着 data=[{}] 没有使用我的代码中的 simple_string_parameters 发送。

最佳答案

Tried lot of way to send data using HttpPost but not succeed

=> 是的,这很明显,因为您的网络服务 URL 遵循的是 GET 方法,而不是 POST。

http://xxxxx/xxx/abc.php?id=xx&name=xxx&data=[ {.....}]

所以我建议您尝试使用 HTTPGet 方法而不是 HTTPPost

检查 adding parameters to a HTTP GET request in Android 的答案?

关于android - 在 android 中使用 HttpPost 发布 JsonArray 和字符串参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16684405/

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