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?

asked Apr 23, 2014 at 17:22
1

2 Answers 2

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();
 }
answered Apr 29, 2014 at 12:22
Sign up to request clarification or add additional context in comments.

Comments

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");
answered Aug 20, 2014 at 6:28

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.