【问题标题】:Volley String post request from a curl command来自 curl 命令的 Volley String 发布请求
【发布时间】:2019-03-21 12:14:19
【问题描述】:

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

$ curl "https://api.*****.com/auth/oauth/v2/token" \

--不安全的\

--header "接受: application/json" \

--header "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。但这并没有解决我的问题。再次感谢

【问题讨论】:

    标签: android-volley


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      我遇到了类似的问题。 将 --data 值放入 body 为我解决了。

      【讨论】:

        猜你喜欢
        • 2021-01-10
        • 2020-10-01
        • 1970-01-01
        • 2019-07-07
        • 1970-01-01
        • 1970-01-01
        • 2011-09-18
        • 2017-10-27
        • 1970-01-01
        相关资源
        最近更新 更多