com.android.volly.AuthFailureError向django服务器发出基本的volly POST请求

时间:2015-08-08 09:21:37

标签: android post android-volley

我正在尝试从我的Android应用程序连接到django服务器。我正在尝试访问我的api,用volly发出POST请求。一切都准备好了。所有的参数和标题都需要,但我仍然得到这个错误。

log: [490] BasicNetwork.performRequest: Unexpected response code 401 for https://example.com/

它不让我访问我的django Api。它适用于PHP服务器。

    public void vollyRequest()
    {
        RequestQueue queue  = Volley.newRequestQueue(this);
        StringRequest request =  new StringRequest(Request.Method.POST  , "https://example.com/", new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                Toast.makeText(MainActivity.this, "RESPONSE: " + response, Toast.LENGTH_SHORT ).show();
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(MainActivity.this, "ERROR: " + error, Toast.LENGTH_SHORT ).show();
            }
        }) {

            @Override
            protected Map<String,String> getParams(){
                Map<String,String> params = new HashMap<String, String>();
                params.put("username","***");
                params.put("password","***");
                return params;
                }
//            @Override
//            public Map<String, String> getHeaders() throws AuthFailureError {
//            Map<String,String> params = new HashMap<String, String>();
//            params.put("Content-Type","application/x-www-form-urlencoded");
//            return params;
//            }
        };
        queue.add(request);
    }

2 个答案:

答案 0 :(得分:10)

我自己解决了...... 此代码将为您提供一个auth-token,其中包含您提供的用户名和密码,然后该标记将放入标题中以从服务器获取数据...

public void vollyRequestGetAuthToken()
{
    RequestQueue queue  = Volley.newRequestQueue(this);
    StringRequest request =  new StringRequest(Request.Method.POST  , "https://example.com/get-auth-token/", new Response.Listener<String>() {

        @Override

    public void onResponse(String response) {
        Toast.makeText(MainActivity.this, "RESPONSE: " + response, Toast.LENGTH_SHORT ).show();
        System.out.println("RESPONSE:          >>> " + response + "<<<");
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Toast.makeText(MainActivity.this, "ERROR: " + error, Toast.LENGTH_SHORT ).show();
    }
}) {
    @Override
    protected Map<String,String> getParams(){
        Map<String,String> params = new HashMap<String, String>();
        params.put("username","*****");
        params.put("password","*****");
        return params;
    }
    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String,String> params = new HashMap<String, String>();
        params.put("Authorization",
                String.format("Basic %s", Base64.encodeToString(
                        String.format("%s:%s", "<username>", "<password>").getBytes(), Base64.DEFAULT)));
        params.put("username" , "*****" );
        params.put("password" , "*****" );
        return params;
    }
};
    queue.add(request);
}

现在,当您拥有auth-token时,请使用以下代码发送auth-token以获取数据

@Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("Authorization", "Token <token>");
                return headers;
            }

答案 1 :(得分:1)

这是由于从API端点拒绝/阻止请求引起的。 原因可能是请求格式错误,

  • 检查您的请求类型[GET / POST /其他]
  • 检查请求的标题和正文