简体   繁体   中英

How to send params to json array request in android volley

I have a students table.

I am trying to send request to and REST APP using the HTTPS and JSON Array from the Android studio to My web-based application.

My request works fine.

The problem I am getting is how to send params in the request.

public void SyncRoutsAfterExport(){
    //Send request to server to get routes
    //Request que
    RequestQueue mQueue =  Volley.newRequestQueue(UserProfile.this);
    //Json perse function
    String url = "xxxxxxxxxxxxxxxxxx";

    Map<String, String> params = new HashMap<String, String>();
    params.put("name", "mark");
    params.put("nam", "someOtherVal");

    JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            for (int i = 0; i < response.length(); i++){
                try {
                    JSONObject jresponse = response.getJSONObject(i);
                    String name= jresponse.getInt("name");
                    String age= jresponse.getString("age");


                    AddDatatotable(name,age);
                    if(i == response.length() -1){

                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });

    mQueue.add(request);
}

so In the above code, I want to send couple of params name and age. How to send the above request with params.

I think you can try StringRequest and overload getParams Method

RequestQueue mQueue =  Volley.newRequestQueue(UserProfile.this);
    //Json perse function
    String url = "xxxxxxxxxxxxxxxxxx";

    Map<String, String> params = new HashMap<String, String>();
    params.put("name", "mark");
    params.put("nam", "someOtherVal");

    StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String requestResponse) {
            JSONArray response
            try {
             array=new JSONArray(requestResponse);
        } catch (JSONException e) {
            e.printStackTrace();
        }
            for (int i = 0; i < response.length(); i++){
                try {
                    JSONObject jresponse = response.getJSONObject(i);
                    String name= jresponse.getInt("name");
                    String age= jresponse.getString("age");


                    AddDatatotable(name,age);
                    if(i == response.length() -1){

                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    }){
@Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return params;
            }
};

    mQueue.add(request);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM