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);
}
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.
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.
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.
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With