如何使用Volley Library将Json对象作为请求发布并在android中获取jsonArray?

时间:2018-09-14 07:33:48

标签: android android-volley

我是Volley Libary的新手,您可以指导如何获取JSONObject的JSONArray响应作为请求。

2 个答案:

答案 0 :(得分:0)

    RequestQueue MyRequestQueue = Volley.newRequestQueue(Register.this);

    String u = your url;
    StringRequest MyStringRequest = new StringRequest(Request.Method.POST, u, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {



                try {

                    JSONObject res = new JSONObject(response);  // getting response object
                    JSONObject data = res.getJSONObject("Response");
                    login_userid = data.getString("_id");    //get the string
                    Ins_name = data.getString("Inscube_Name");
                    User_Email = data.getString("Email");
                    Color_code = data.getString("Color_Code");

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




            }
        }
    }, new Response.ErrorListener() { //Create an error listener to handle errors appropriately.
        @Override
        public void onErrorResponse(VolleyError error) {

            Toast.makeText(Register.this, " " + error, Toast.LENGTH_SHORT).show();
        }
    }) {
        protected Map<String, String> getParams() {
            Map<String, String> para = new HashMap<String, String>();

            para.put("",my);   //put your post string here
            para.put("", reg_email);
            para.put("", reg_pass);
            para.put("", my);

            return para;
        }
    };

    MyStringRequest.setRetryPolicy(new DefaultRetryPolicy(8000, 3, 2));

    MyRequestQueue.add(MyStringRequest);

}

答案 1 :(得分:0)

看起来您可以轻松地修改此构造函数并将其添加到JsonArrayRequest

public JsonArrayRequest(int method, String url, JSONObject jsonRequest,
                            Listener<JSONArray> listener, ErrorListener errorListener) {
        super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener,
                errorListener);
    }