android - 排球请求问题

时间:2015-09-29 07:55:54

标签: android android-volley

我需要发送此请求:

curl -i -X POST --insecure -H "Content-Type: application/x-www-form-urlencoded" -d "user=abc" -d "code=ddd" https://my.url.com/auth

HTTP Post请求的构造

POST /auth HTTP/1.1
Host: my.url.com
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded

user=abc&code=ddd

我使用排球库。我试着这样做:

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST, url, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject json) {
                // Display the first 500 characters of the response string.

                try {
                    if (json.getBoolean("success"))
                    {
                        openContactView();
                    }
                    else {
                        setStatus("Error. Server down or access denied");
                    }
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    setStatus("Error. Server down or access denied");
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(), "error = " + error.toString(), Toast.LENGTH_LONG).show();
                setStatus("Error. Server down or access denied");
            }
        }){
             @Override
                public String getBodyContentType() {
                    return "application/x-www-form-urlencoded; charset=UTF-8";
                }

             @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("user", "abc");
                    params.put("code", "ddd");
                    return params;
                }
        };

        // Add the request to the RequestQueue.
        queue.add(jsonObjReq);

但是截击请求不等于卷曲请求。我从服务器得到不同的回应。有什么区别?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案

@Override
            public byte[] getBody() {
                // TODO Auto-generated method stub
                return "user=abc&code=ddd".getBytes();
            }

代替

  @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String, String> params = new HashMap<String, String>();
                        params.put("user", "abc");
                        params.put("code", "ddd");
                        return params;
                    }

为我工作