如何使Android Volley请求与身体如下

时间:2019-05-17 10:02:08

标签: android android-volley

我在邮递员中有以下要求,我想在排球android中实现它。

有解决方案吗?

以下是请求标头

request body

以下是请求正文

request header

2 个答案:

答案 0 :(得分:1)

执行此操作。添加标头数据。

RequestQueue queue = Volley.newRequestQueue(this);
        String url = APICLient.BASE_URL+"api/Admin_controller/getProspectField";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jo=new JSONObject(response);
                            Boolean status=jo.getBoolean("success");

                            JSONArray ja=jo.getJSONArray("user");

                             // write your logic here...
                            for (int i=0;i<ja.length();i++){
                                JSONObject jsonObject=ja.getJSONObject(i);
                                String pname=jsonObject.getString("p_name");
                                String pType=jsonObject.getString("p_type");


                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders(){
                HashMap <String,String> params=new HashMap<>();
                params.put("authorization","put_authorization_key_here");

                return params;
            }

            @Override
            public Map<String, String> getParams(){
                HashMap <String,String> params=new HashMap<>();
                params.put("care_team[]",team);
                ........................
                return params;
            }
        };
        queue.add(stringRequest);

希望这会起作用。

答案 1 :(得分:0)

Json用截击进行解析:

private void userLogin() {

    StringRequest stringRequest = new StringRequest(Request.Method.POST, URLs.URL_LOGIN,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {

                                try {
                                    //converting response to json object
                                    JSONObject obj = new JSONObject(response);

                                    //if no error in response
                                    if (obj.getBoolean("status")) {
                                        Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();

                                        //getting the user from the response
                                        JSONObject userJson = obj.getJSONObject("user");

                                        //creating a new user object
                                        User user = new User(
                                                userJson.getInt("cid"),
                                                userJson.getInt("company_id"),
                                                userJson.getString("name"),
                                                userJson.getString("email")

                                        );

                                        //storing the user in shared preferences
                                        SharedPreferenceManager.getInstance(getApplicationContext()).userLogin(user);

                                        //starting the profile activity
                                        finish();
                                        startActivity(new Intent(getApplicationContext(), MainActivity.class));
                                    } else {
                                        Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
                                        progressBar.setVisibility(View.GONE);

                                    }

                                } catch (JSONException e) {
                                     e.printStackTrace();
                                    progressBar.setVisibility(View.GONE);
                                }
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                                progressBar.setVisibility(View.GONE);
                            }

                        })


                {
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String, String> params = new HashMap<>();
                        params.put("company_id",companyid );
                        params.put("email", username);
                        params.put("password", password);
                        return params;
                    }


                    @Override
                    public Map<String, String> getHeaders() throws AuthFailureError {
                        HashMap<String, String> headers = new HashMap<String, String>();
                        headers.put("x-api-key", "swicons@1019");
                        return headers;
                    }

                };

                stringRequest.setShouldCache(false);
        VolleySingleton.getInstance(this).addToRequestQueue(stringRequest);
            }