#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
Related
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;
}
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.
I need to pass two parameters to server as cookie and I'm using volley library.
I researched a lot and i couldn't find any solutions.
Below is my GET request to server.
StringRequest srequest = new StringRequest(method, URL, stringlistener, errorListener) {
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
return params;
}
};
ApplicationController.getInstance().addToRequestQueue(srequest);
And I've set CookieHandler also like below,
CookieStore cookieStore = new CustomCookieStore();
cookieStore.add(URI.create(URL), new HttpCookie(Constants.DEVICE_ID, a_id));
cookieStore.add(URI.create(URL), new HttpCookie(Constants.APP_VERSION_CODE, "100"));
CookieManager cookieManager = new CookieManager(cookieStore, CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(cookieManager);
Without cookie, it is working perfectly. And we can't avoid cookies at this stage. So please help me on this.
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.
I have one small strange question: How do I set the user agent in Volley?
I need the full version of some sites (desktop version), not mobile version.
I tried to change variable userAgent from "volley/0" to something like "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36" (my Chrome). No difference.
Any advice?
You should override the method getHeaders() in Request and set the "User-agent" header
In your Request class:
#Override
public Map<String, String> getHeaders(){
Map<String, String> headers = new HashMap<String, String>();
headers.put("User-agent", "YOUR_USER_AGENT");
return headers;
}
Following on from #alex's answer you need to add this #Override function to your request object when adding it to the queue.
Request request = new Request(
Method.GET,
url,
Listener listener,
ErrorListener errorListener) {
#Override
public Map<String, String> getHeaders(){
Map<String, String> headers = new HashMap<String, String>();
headers.put("User-agent", "YOUR_USER_AGENT");
return headers;
}
};
To understand more about how to add this to your requests, see this StackOverflow answer about setting headers. - How to set custom header in Volley Request