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.
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;
}
#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
I'm using Volley to make synchronous requests to a server. The following is sample how i make synchronous request with volley.
RequestFuture<JSONObject> future = RequestFuture.newFuture();
JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, url + param, null, future, future);
RequestQueue queue = NetworkSingleton.getInstance(ctx).getRequestQueue();
queue.add(req);
How can i add headers to request?
When i make async request with volley, i can simple override getHeaders method, but i really don't know how to do that with request future.
I haven't tried running this but I put it into my application and it didn't throw any errors or warnings.
RequestFuture<JSONObject> future = RequestFuture.newFuture();
JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, "url", null, future, future){
#Override
public HashMap<String, String> getHeaders() {
String token = ApplicationController.getToken();
HashMap<String, String> params = new HashMap<>();
params.put("Authorization", "token");
params.put("Content-Type", "application/json;charset=UTF-8");
params.put("Accept", "application/json");
return params;
}
};
Since you're still doing a JSONObjectRequest, I think you can still override getHeaders in the JSONObjectRequest like you usually do.
How do I send a DELETE Request using Volley (Android) to a REST Api with parameters like access_token and username. I have tried almost everything on Internet but still I am getting a 400 error Message.
I have tried sending the same DELETE request using PostMan and it works perfectly fine.
Here is the Error:
E/Volley﹕ [1112] BasicNetwork.performRequest: Unexpected response code 400 for http://myserverip/messages/2
and Here is the Java code that I am using:
String URI = "http://myserverip/messages/2";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.DELETE,URI,null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Toast.makeText(contextm,response.toString(),Toast.LENGTH_SHORT).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
}){
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = super.getHeaders();
if (headers == null
|| headers.equals(Collections.emptyMap())) {
headers = new HashMap<String, String>();
}
headers.put("access_token", "access_token");
headers.put("username", "username");
return headers;
}
};
MyApp.getInstance().addToRequestQueue(request);
Might not be really useful but here is the working DELETE request preview from PostMan
DELETE /messages/1 HTTP/1.1
Host: serveripaddress
Cache-Control:no-cache
Content-Type: application/x-www-form-urlencoded
access_token=SPAVYaJ7ea7LzdeQYrBTsIRssuGbVpJI8G9XSV0n&username=whit3hawks
You can easily achieve this if you put the following instead of null in ... new JsonObjectRequest(Request.Method.DELETE,URI,null,new Response.Listener<JSONObject>() { ...
final JSONObject object = new JSONObject();
try {
object.put("access_token", "SPAVYaJ7ea7LzdeQYrBTsIRssuGbVpJI8G9XSV0n");
object.put("username", "whit3hawks");
} catch (Exception e) {
e.printStackTrace();
}
So your Request should look like this:
... new JsonObjectRequest(Request.Method.DELETE,URI,object,new Response.Listener<JSONObject>() { ...
You posted a long time ago, but maybe someone else will need this some time.
I am successfully sending headers in volley while using DELETE with this code:
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
return headers;
just tried it and works like a charm.
EDIT:
if you're using a Mac, try checking with Charles what exactly it is you are sending,
on a PC you can try fiddler.
also, error 400 means 'Bad Input', so the server is either getting more info then it should, or less then he expects.
and keep in mind that DELETE in Volley acts as a GET, make sure you are sending an empty body with the request.
hope that helped somehow.. happy coding
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.