使用凌空发布阵列

时间:2016-03-12 14:22:54

标签: android android-volley

我想在我的PHP文件中使用volley发布类似这样的内容[1,2,14,15],其中有一个SQL可以从数据库中检索数据。

我的排球:

  Map<String, String> userParams = new HashMap<String, String>();
            listParams.put(TAG_IDS, all_ids);

            CustomRequest myREquest= new CustomRequest(Request.Method.POST, URL, listParams,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {

在我的PHP文件中,我将使用这样的SQL:

$liste=$_POST['IDS'] ;
$val= array_map('intval', $liste);
        $new_liste = implode("','", $val);    
$query = "SELECT * table WHERE IDS IN('".$new_liste ."')";

这里是我的CustomRequest

public class CustomRequest extends Request<JSONObject> {

    private Response.Listener<JSONObject> listener;
    private Map<String, String> params;

    public CustomRequest(String url, Map<String, String> params, Response.Listener<JSONObject> reponseListener,
                         Response.ErrorListener errorListener){
        super(Method.GET, url, errorListener);
        this.listener=reponseListener;
        this.params= params;
    }

    public CustomRequest(int methode, String url, Map<String, String> params, Response.Listener<JSONObject> reponseListener,
                         Response.ErrorListener errorListener){
        super(methode,url,errorListener);
        this.listener = reponseListener;
        this.params = params;
    }

    protected Map<String, String> getParams()
        throws com.android.volley.AuthFailureError{
        return params;
    }

    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {

        try {
            String jsonString = new String(response.data,
                    HttpHeaderParser.parseCharset(response.headers));
            return Response.success(new JSONObject(jsonString),
                    HttpHeaderParser.parseCacheHeaders(response));

        }catch (UnsupportedEncodingException e){
            return Response.error(new ParseError(e));
        }catch (JSONException je){
            return Response.error(new ParseError(je));
        }
    }

    @Override
    protected void deliverResponse(JSONObject response) {
        listener.onResponse(response);
    }
}

请帮忙。

1 个答案:

答案 0 :(得分:0)

您应该迭代all_ids并将所有这些内容放在Map中,然后发送到服务器。例如:

            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<>();

                for(int i=0; i<all_ids.size(); i++) {
                   params.put(TAG_IDS + "[" + i + "]", all_ids.get(i).toString());
                }

                return params;
            }