Android Volley 请求 - 将对象发送到 Spring 引导 java Web 服务
Android Volley request - send object to Spring boot java web service
我有一些习惯POJO
:
class CustomClass {
int x;
String str;
SecondCustomClass obj; //indicate it's not class of simple types
//etc...
}
我想将他的实例从 Android
(Volley 库)客户端发送到 Web 服务 运行 Spring-boot
java 申请。目前我知道如何使用 URL
参数和 return 将数据发送到客户端自定义对象。但我也想发送自定义对象。
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 object.Below就是例子。当您仅通过一个请求请求参数时。
这是请求 JSON
{
"x":10,
"str":"MyName",
"SecondCustomClass":{
"id":10,
"title":"make it eassy"
}
}
这是来自 Android 的 post 参数请求。试试这个方法。
更多详情 please use this link
我有一些习惯POJO
:
class CustomClass {
int x;
String str;
SecondCustomClass obj; //indicate it's not class of simple types
//etc...
}
我想将他的实例从 Android
(Volley 库)客户端发送到 Web 服务 运行 Spring-boot
java 申请。目前我知道如何使用 URL
参数和 return 将数据发送到客户端自定义对象。但我也想发送自定义对象。
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 object.Below就是例子。当您仅通过一个请求请求参数时。
这是请求 JSON
{
"x":10,
"str":"MyName",
"SecondCustomClass":{
"id":10,
"title":"make it eassy"
}
}
这是来自 Android 的 post 参数请求。试试这个方法。 更多详情 please use this link