简体   繁体   中英

Sending JSON Dictionaries in POST Body Field using Volley

I have been trying to replicate the following curl request in Android Studio using volley for hours and keep getting errors:

curl -X POST -H "Authorization: Bearer <access_token> " \
     -H "Content-Type: application/json" \
     -d '{"ride_type" : "lyft", "origin" : {"lat" : 34.305658, "lng" : -118.8893667 } }' \
     'https://api.lyft.com/v1/rides'

The function is called by running:

runPost(lyftCodeURL + "rides");

The following is my volley code. It always results in a 400 Server response and the error is 'com.android.volley.ServerError'. Please let me know what I am doing wrong. Thank you!

private void runPost(String uri) {
        StringRequest stringRequest = new StringRequest(Request.Method.POST, uri,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        JSONObject obj = null;
                        try {
                            String res = stripHtml(response);
                            obj = new JSONObject(res);
                            tv.setText("res" + res.toString());
                        } catch (Exception e) {
                            tv2.setText(e.toString());
                        }
                        if (obj != null) {
                            handleLyftInfos(obj, 1);
                        }
                    }
                },
                new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        tv.setText("err" + error.networkResponse.statusCode + " mfg " +  error.toString());
                    }
                }
        ) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                JSONArray jArrayInput=new JSONArray();
                JSONObject originObject = new JSONObject();
                try {
                    params.put("ride_type", "lyft");
                    originObject.put("lat", "34.305658");
                    originObject.put("lng", "-118.8893667");
                    params.put("origin", originObject.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Log.d("Param:", params.toString());
                return params;
            }
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String,String> params = new HashMap<String,String>();
                params.put("Authorization","Bearer " + aKey); params.put("Content-Type","application/json");
                return params;
            }
        };
        queue.add(stringRequest);

    }

Have a look at JsonObjectRequest.

JSONObject request = new JSONObject();
request.put("a","1");
request.put("v","33");

JsonObjectRequest strReq = new JsonObjectRequest(Request.Method.POST, "<URL>", request,
        new Response.Listener<JSONObject>() {

             @Override

             public void onResponse(JSONObject response) {
             },
        new Response.ErrorListener() {

             @Override
             public void onErrorResponse(VolleyError error) {
             }
        });

I am not sure if the above answer also works, but I figured out another solution to my problem. I had to override the getBody() and getBodyContentType() functions rather than getParams(), in case anyone else comes across this problem.

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