使用Android Volley将数组发布到PHP

时间:2015-04-05 18:09:07

标签: php android mysql android-volley

这是我用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 [&#39; params&#39;]发送给自己时,看看它是什么样的,我得到了

{"params_0":"000000000284","params_1":"000000000229","params_2":"000000000081"}

如何单独处理这些项目?

2 个答案:

答案 0 :(得分:1)

我刚遇到同样的问题。我的解决方案:

Map<String, String> params = new HashMap<String, String>();
   params.put("params[0]", "a");
   params.put("params[1]", "b");
   params.put("params[2]", "c");

答案 1 :(得分:0)

Map<String, String> params = new HashMap<String, String>();

params.put("params[0]", "a");

params.put("params[1]", "b");

params.put("params[2]", "c");