我正在尝试使用 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/