2

I have some custom POJO:

class CustomClass {
    int x;
    String str;
    SecondCustomClass obj;  //indicate it's not class of simple types  
    //etc...
}

I want to send instance of him from Android (Volley library) client to web service running on Spring-boot java application. Currently I know how to send data with URL params and return to client custom object. But I want also to send custom object.

Code in Android (I know that I need to use 3'rd parameter which is now null but I'm struggling get it work):

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,
    "BASE_URL?param1=param", 
    null,
    new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            CustomClass result = new Gson().fromJson(response.toString(), CustomClass.class);
        }
    },
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    }
);
volleyQueue.add(request);

Code in server:

EDIT: The solution to receive pojo is using @RequestBody

@RequestMapping("/webmethod")
public CustomClass webmethod(@RequestParam(value="obj_param") @RequestBody CustomClass obj) {
    //work with obj
}

What do I need to put in Android side to get it work?

michael
  • 3,835
  • 14
  • 53
  • 90

1 Answers1

1

You have to use the JSON object inside the JSON object.Below is the sample. When you are request the Parameter with only one request.

This the Request JSON

  {
    "x":10,
     "str":"MyName",
     "SecondCustomClass":{
          "id":10,
           "title":"make it eassy"
        }  
   }

This is the post parameter request from Android. Try this way. For more details please use this link

Community
  • 1
  • 1
Maheshwar Ligade
  • 6,709
  • 4
  • 42
  • 59