Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

com.android.volly.AuthFailureError in making basic volly POST request to a django server

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/

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);
    }
like image 988
Nyob Avatar asked Aug 08 '15 09:08

Nyob


People also ask

How do I make volley HTTP requests?

Unfortunately, there is almost no documentation on Volley. So, I put together code snippets on how to make Volley HTTP Requests (GET, POST, PUT, DELETE). Setting up is straight-forward. Clone the Volley project from here and then import the Volley into project.

How to get data from API in Android using Android volley?

Below is the dependency for Volley which we will be using to get the data from API. For adding this dependency navigate to the app > Gradle Scripts > build.gradle (app) and add the below dependency in the dependencies section. After adding this dependency sync your project and now move towards the AndroidManifest.xml part.

What is Google volley and how does it work?

At Google I/O this year, Google announced a library it had built called Volley. The goal of Volley is to further simplify and standardize the network request process in Android development.

What is overriding the getparams method in a volley request?

Overriding the getParams method allows you to build the HashMap and return the object to the Volley request for posting. Similarly, if you need to add any headers to the request, you override the getHeaders method and build/return your key,value pairs in a HashMap there as well. The major shortcoming of Volley is its lack of documentation.


1 Answers

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

and now when you have the auth-token, use the following code to send auth-token to get data

@Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("Authorization", "Token <token>");
                return headers;
            }
like image 72
Nyob Avatar answered Oct 02 '22 21:10

Nyob