Android volley library - what is the use of getHeaders() & getParams()? - android

I am a newbie to Android. I have studied Android volley library, and I have doubted in getHeaders() and getParams() because those two methods post values into webservice.
What is the difference on those methods, and what is the reason for using getHeaders()?

getParams():
To POST values to the server, you can simply store the values in a HashMap as key-value pairs. Overriding the getParams() method allows you to build the HashMap and return the object to the Volley request for posting.
#Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("user", "Android");
params.put("pass", "123456");
return params;
}
getHeaders():
If you need to add any headers to the request, you can override the getHeaders() method and build/return your key-value pairs in a HashMap there as well.
#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;
}

getHeaders() is the method you have to override to pass the headers of your request and getParams() is the same for POST/PUT requests params.

Related

passing auth token as header format from client to remote server

JSON response from a remote server
{"status":"success",
"data":{"auth_token":"9389e656c90e11c451443657c8e",
"user":{"active_location":" Airport"}}}
I need to store the auth_token and pass to the remote server as the header,
tried addHeader("key1", "value1");
but still not working, need help
To send headers for belly request, you need to override getHeaders() method. Inside method create a map and put your key-value pairs and return the the map.
JsonObjectRequest request = new JsonObjectRequest(requestMethod, yourUrl, postData(if any), new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
//TODO parse your response
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//TODO handle error
}
}){
//Here is the place where you can add your headers
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("key1", "value1");
return params;
}
};
Also if you need to pass any request parameters you can do that in the same way by overriding getParams() method.
For more details check the volley tutorial
You can simply use Base64 to encode the token string and put it in SharedPrefs, then read and decode to send in a header.

How to convert the headers key to uppercase?

The headers key added by calling webview.loadUrl(String url, Map<String, String> additionalHttpHeaders) is finally converted to lowercase, and how to convert to uppercase.
HashMap<String, String> headers = new HashMap<>();
headers.put("Token", "dfds343");
webView.loadUrl("www.google.com", headers);

DELETE with parameter using Volley

I am getting HTTP error 422, while making a DELETE request using volley. But, I am able to success response when I make request on Postman. Also, when I tried with HTTPConnection again got the same error.
Here is my code of volley request
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.DELETE, SERVER_URL,
postPropertyJSONObject, responseListener, errorListener);
you can refer https://github.com/ngocchung/DeleteRequest for long term solution, as it looks from documentation, but I have not tested this. A quick work arrount is to, to make request as post and then overwrite the header X-HTTP-Method-Override as DELETE.
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST, SERVER_URL,postPropertyJSONObject, responseListener, errorListener);
and then add Header like this
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String> ();
headers.put("X-HTTP-Method-Override", "DELETE");
headers.put("Accept", "application/json");
headers.put("Content-Type", "application/json");
return headers;
}

Why Volley Send Cookie in Header Not Working Android?

#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
headers.put("Authorization", Auth_token);
headers.put("Cookie", GlobleVariables.COOKIE_VALUE + Auth_token);
return headers;
}
If you do not need compatibility with Android < 2.3 you just need to add this line of code in your onCreate of the activity or the application. That will activate default cookieManager for all httpURLconnections.
CookieHandler.setDefault(new CookieManager());
Source: Volley ignores Cookie header request
And you can find detail solution Here

Using volley library, cant execute a post method form with parameters

I am working on volley library: http://developer.android.com/training/volley/index.html
Get and 'Post methods without parameters' working fine. But when parameters are given, volley does not execute the form, and acts like form itself is a jsonObject:
com.android.volley.ParseError: org.json.JSONException: Value Login< of type java.lang.String cannot be converted to JSONObject
I have tried both overriding getParams() method:
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("username", username);
params.put("password", password);
return params;
}
And instantiating the object with parameter:
Map<String, String> params2 = new HashMap<String, String>();
params2.put("username", username);
params2.put("password", password);
JsonObjectRequest jsonObjectRequest1 = new JsonObjectRequest(Request.Method.POST, LOGIN_URL, new JSONObject(params2), new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
//onResponse
}
});
None of them worked. I am guessing my problem is about the content types. Volley library uses application/json while my php codes use name-value pairs.
I have seen these two questions but sadly they did not solve my case:
Google Volley ignores POST-Parameter
Volley Post JsonObjectRequest ignoring parameters while using getHeader and getParams
When you use JsonObjectRequest, you are saying that the content you are posting is a JSON Object and the response you expect back will also be a JSONObject. If neither of these are true, you need to build your own Request<T> and set the values you need.
The error you are seeing is because the response from the server is not a valid JSON response.

Categories

Resources