我尝试将数组发布到 PHP ,但是当我使用下面的代码时,没有任何内容被转移,$_POST
数组会显示为{}
。我怎样才能将数组发布到php?
MultipartRequest request = new MultipartRequest();
request.setUrl(url);
request.setPost(true);
String[] value = {"a","b","c"};
request.addArgument(argument, value);
谢谢!
答案 0 :(得分:2)
如果您没有附加文件而不是Java
,请使用$apaiIO = new ApaiIO($conf);
$search = new Search();
$search->setCategory('Books');
$search->setCondition('All');
$search->setKeywords('Hadoop Java');
:
ConnectionRequest
使用json库添加数组:
MultipartRequest
然后将其作为参数添加到您的请求中:
ConnectionRequest request = new ConnectionRequest();
request.setUrl(url);
request.setPost(true);
答案 1 :(得分:2)
@tizbn提供的答案可能在某种程度上有效,但并不总是与php一起使用,所以我使用了StringBuilder
,如下所示,但此解决方案要求您事先知道数组的大小:< / p>
...
List list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
String[] value = new String[list.size()];
for (int i = 0; i < list.size(); i++) {
StringBuilder builder = new StringBuilder();
value[i] = (builder.append("\"").append(list.get(i)).append("\"")).toString();
}
Arrays.toString(value);
ConnectionRequest request = new ConnectionRequest();
request.setUrl(url);
request.setPost(true);
request.addArgument("arg", value);
...
在php端,您可以阅读以下值:
...
$arg = isset($_POST['arg']) ? $_POST['arg'] : '';
$argArray = json_decode($arg);
foreach ($argArray as $value) {
...
// do whatever you want with $value
}
...