I have been trying to post my Credentials class from Android to C#.Net web server.
Volley Post method accepts params like:
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return parameters;
}
Return type of getParams() is Map<String, String> but I need Map<String, Object> to send my class to web server. I even tried to convert my class into json string like:
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("credentials", new Gson().toJson(mCredentials, Credentials.class));
return parameters;
}
But it does not work. Server returns "Invalid Parameter" error which is thrown when "credentials" parameter is null.
There is nothing wrong on server side because I was able to do it with AsyncTask. I decided to turn my requests into Volley and I got stuck on this problem.
Anybody has a solution?
-
possible answer in SO question stackoverflow.com/questions/29779447/…TommySM– TommySM2015年04月22日 09:35:56 +00:00Commented Apr 22, 2015 at 9:35
2 Answers 2
This is how I do it (note: params is a jsonobject, not a map):
//fill params
JSONObject params = new JSONObject();
params.put("username", username);
params.put("password", password);
//create future request object
RequestFuture<JSONObject> future = RequestFuture.newFuture();
//create JsonObjectRequest
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST, URL, params, future, future);
//add request to volley
MyVolley.getRequestQueue().add(jsObjRequest);
//pop request off when needed
try {
JSONObject response = future.get();//blocking code
} catch (Exception e) {
e.printStackTrace();
}
Comments
If you need to have your package send something like an object, you can put a JSONObject within a JSONObject. Eg
JSONObject outer = new JSONObject();
JSONObject inner = new JSONObject();
inner.put("first_name","James");
inner.put("last_name","Bond");
outer.put("person",inner);
outer.put("id","1");