当前位置: 代码迷 >> Android >> 使用Volley在POST正文字段中发送JSON词典
  详细解决方案

使用Volley在POST正文字段中发送JSON词典

热度:58   发布时间:2023-08-04 11:16:30.0

我一直在尝试使用齐射在Android Studio中复制以下curl请求几个小时,并不断出错:

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'

该函数通过运行来调用:

runPost(lyftCodeURL +“ rides”);

以下是我的齐射代码。 它总是导致400服务器响应,错误是'com.android.volley.ServerError'。 请让我知道我在做什么错。 谢谢!

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

    }

看看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) {
             }
        });

我不确定上述答案是否也有效,但是我想出了解决我问题的另一种方法。 我必须重写getBody()和getBodyContentType()函数,而不是getParams(),以防其他人遇到此问题。

  相关解决方案