向django服务器发出基本volly POST请求时出现com.android.volly.AuthFailureError [英] com.android.volly.AuthFailureError in making basic volly POST request to a django server

查看:99
本文介绍了向django服务器发出基本volly POST请求时出现com.android.volly.AuthFailureError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to connect to a django server from my android application. I am trying to access my api, making a POST request with volly. Everything is set. All the params and headers required, but still I get this error.

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

这不是让我访问我的django Api.它在PHP服务器上正常工作.

It's not letting me access my django Api. It works fine with the PHP server.

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

推荐答案

我自己解决了... 此代码将为您提供一个身份验证令牌,并提供您提供的用户名和密码,然后将该令牌放入标头中以从服务器获取数据...

i solved it my self... This code will get you an auth-token, with the username and password you provide, and then that token will be put in the header to get data from the server...

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

现在,当您拥有身份验证令牌时,请使用以下代码发送身份验证令牌以获取数据

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

这篇关于向django服务器发出基本volly POST请求时出现com.android.volly.AuthFailureError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆