简体   繁体   English

如何在android volley中将参数发送到json数组请求

[英]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.我正在尝试使用 HTTPS 和 JSON 数组从 Android Studio 向基于 Web 的应用程序发送请求和 REST APP。

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我认为您可以尝试 StringRequest 并重载 getParams 方法

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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