我有一些自定义的 POJO
:
class CustomClass {
int x;
String str;
SecondCustomClass obj; //indicate it's not class of simple types
//etc...
}
我想将他的实例从 Android
(Volley 库)客户端发送到在 Spring-boot
java 应用程序上运行的 Web 服务。目前我知道如何使用 URL
参数发送数据并返回到客户端自定义对象。但我还想发送自定义对象。
Android 中的代码(我知道我需要使用现在为 null
的第 3 个参数,但我正在努力让它工作):
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:
编辑:接收 pojo 的解决方案是使用 @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?
最佳答案
您必须在 JSON 对象中使用 JSON 对象。下面是示例。当您仅通过一个请求请求参数时。
这是请求 JSON
{
"x":10,
"str":"MyName",
"SecondCustomClass":{
"id":10,
"title":"make it eassy"
}
}
这是来自Android的post参数请求。试试这个方法。 更多详情 please use this link
关于Android Volley 请求 - 将对象发送到 Spring boot java web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34251321/