java - 在 HTTP POST 中设置参数

标签 java android http

我正在尝试使用 HttpClient API 对服务器进行 JSON 调用。代码sinppet如下所示。

HttpClient httpClient = new DefaultHttpClient();
HttpGet httpPost = new HttpPost(URLString);
HttpResponse response = httpClient.execute(httpPost);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);  
nameValuePairs.add(new BasicNameValuePair("method", "completeUserLogin")); 
String[] params = new String[]{"100408"};
response = httpClient.execute(httpPost);

我想将参数添加到 nameValuePairs。 BasicNameValuePair 类不允许您添加数组。有什么想法吗?

提前致谢!

最佳答案

看看这个。在这里,他们在 BasicNameValuePairs 中传递数组。这里的颜色是我们要在服务器上发送的数组。您应该使用数组变量而不是颜色。

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
nameValuePairs.add(new BasicNameValuePair("colours[0]","red"));  
nameValuePairs.add(new BasicNameValuePair("colours[1]","white"));  
nameValuePairs.add(new BasicNameValuePair("colours[2]","black"));  
nameValuePairs.add(new BasicNameValuePair("colours[3]","brown"));  

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpClient.execute(httpPost);

关于java - 在 HTTP POST 中设置参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12365832/

相关文章:

java.lang.UnsatisfiedLinkError : Couldn't load STLport_shared: findLibrary returned null (tess-two) 错误

java - 为什么 Java 7 为签名的应用程序请求网络权限?

android - NestedScrollView、SwipeRefreshLayout 和WebView 如何一起使用?

android - Google Play 商店 APK - 设备兼容性问题

javascript - 检索 Post Request 响应并将其存储到 Angular 8 中的变量中

java - 将具有嵌套 Java 类数组的 Java 类转换为 JSON

java - Apache CXF 客户端内存

javascript - antiforgeryToken 放在哪里

java - 安卓工作室 : How to start TexttoSpeech after 10 seconds?

http - Netty 4.x客户端文件上传