3

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?

JustWork
  • 1,944
  • 3
  • 22
  • 34
  • possible answer in SO question http://stackoverflow.com/questions/29779447/put-arraylist-into-param-jsonobject/29791979#29791979 – TommySM Apr 22 '15 at 09:35

2 Answers2

0

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();
     }
FriendlyMikhail
  • 2,857
  • 23
  • 39
0

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");
the4960
  • 61
  • 4