[英]Using Android Volley to post an array to PHP
这就是我用Volley发布值的方法:
@Override
protected Map<String, String> getParams() {
JSONObject jsonObjectMembers=new JSONObject();
for (int i=0; i<arr_added_userids.size(); i++) {
try {
jsonObjectMembers.put("params_"+i,arr_added_userids.get(i));
} catch (JSONException e) {
e.printStackTrace();
}
}
Map<String, String> params = new HashMap<String, String>();
params.put("host", session_userid);
params.put("params",jsonObjectMembers.toString());
return params;
}
这是我尝试的:
foreach($_POST['params'] as $key => $value)
{
try {
$stmt = $conn->prepare("INSERT INTO CREWMEMBERS (CREWID, MEMBER) VALUES
(:crewid, :member)");
$query_params = array(
':crewid' => $crewid,
':member' => $value
);
$stmt->execute($query_params);
$response["success"] = 1;
} catch(PDOException $e) {
//echo 'ERROR: ' . $e->getMessage();
$response["success"] = 3;
}
}
我总是得到org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
当我通过电子邮件向自己发送$ _POST ['params']以查看其外观时,我得到
{"params_0":"000000000284","params_1":"000000000229","params_2":"000000000081"}
如何分别处理这些物品?
我刚刚面临同样的问题。 我的解决方案:
Map<String, String> params = new HashMap<String, String>();
params.put("params[0]", "a");
params.put("params[1]", "b");
params.put("params[2]", "c");
Map<String, String> params = new HashMap<String, String>();
params.put("params[0]", "a");
params.put("params[1]", "b");
params.put("params[2]", "c");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.