来自curl命令的Volley String post请求

时间:2015-02-18 16:16:18

标签: android-volley

我正在尝试获取访问令牌。通过使用curl / commnad窗口我可以得到它但我需要使用android排球。 Volley显示错误表示意外的响应代码。这是curl命令:

$ curl“https://api。*****。com / auth / oauth / v2 / token”\

- 不安全\

- 标题“接受:application / json”\

- 标题“Content-Type:application / x-www-form-urlencoded”\

- 数据“grant_type = authorization_code& code = 9d40008a9a4-438b-800c-dec6486a7631”\

- 数据 “client_id = l7xxdedc2c3d49e58459287fe66092ad& client_secret = fa48a352e633841695b1d969750”\

- 数据“scope = scope_test& state = state_test”\

- 数据“redirect_uri = http%3A%2F%2Flocalhost%3A3000”\

- 请求POST $

我写了一个等效的Volley字符串请求。我不知道我的错误在哪里?为什么它没有给我回应。请帮帮我

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

            Log.d("accessToken:",response);
        }
    },
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            Log.d("error:",volleyError.toString());

        }
    }){
        @Override
        public byte[] getPostBody() throws AuthFailureError {
            String grantcode=preference.getAccessToken();
            String httpPostBody="grant_type=authorization_code&code="+grantcode+"&client_id=l7xx5a227281eb364ab3bb6fd8cc49648427&client_secret=c29a24fad90640f993770f07a5e77892&scope=scope_test&state=state_test";
            return httpPostBody.getBytes();
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String,String> headers=new HashMap<String,String>();
            headers.put("Accept","application/json");
            headers.put("Content-Type","application/x-www-form-urlencoded");
            return headers;
        }

    };
            ApplicationController.getInstance().addToRequestQueue(postRequest);
        }
    });

即使我尝试使用getBody而不是getPostBody。但它没有解决我的问题。再次感谢

2 个答案:

答案 0 :(得分:0)

我解决了这个问题。这是方法:

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

            Log.d("accessToken:",response);
        }
    },
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            Log.d("error:",volleyError.toString());

        }
    }){


        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String,String> params=new HashMap<String,String>();
            params.put("grant_type","authorization_code");
            params.put("code",preference.getGrantCode());
            params.put("client_id", Resources.CLIENT_ID);
            params.put("client_secret",Resources.CLIENT_SECRET);
            params.put("scope","scope_test");
            params.put("state","state_test");
            params.put("redirect_uri",Resources.REDIRECT_URI );
            return params;

        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String,String> headers=new HashMap<String,String>();
            headers.put("Accept","application/json");
            headers.put("Content-Type","application/x-www-form-urlencoded");
            return headers;
        }

    };
            ApplicationController.getInstance().addToRequestQueue(postRequest);

答案 1 :(得分:0)

我遇到了类似的问题。 将--data值放入正文对我来说解决了。