Post Array using volley

耗尽温柔 提交于 2019-12-25 18:45:16

问题


I Want to post something like this [1,2,14,15] using volley to my PHP file where there's a SQL to retreive data from database.

Here my volley :

  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) {

And in my PHP file i'll use a SQL something like this :

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

Here my 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);
    }
}

Please help.


回答1:


You should iterate your all_ids and put all of them in Map which will be send to the server. For example:

            @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;
            }


来源:https://stackoverflow.com/questions/35958795/post-array-using-volley

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!