今天写程序的时候遇到了这个问题 记录一下
String url = HTTP_URL+"add";
User user = new User("张三", (long) 1);
String json = JSON.toJSONString(user);
RequestParams params = new RequestParams(url);
params.setAsJsonContent(true);
params.setBodyContent(json);
x.http().post(params, new Callback.CommonCallback<String>() {
@Override
public void onSuccess(String result) {
Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
Toast.makeText(MainActivity.this, ex.toString(), Toast.LENGTH_LONG).show();
}
@Override
public void onCancelled(CancelledException cex) {
Toast.makeText(MainActivity.this, "onCancelled", Toast.LENGTH_LONG).show();
}
@Override
public void onFinished() {
Toast.makeText(MainActivity.this, "onFinished", Toast.LENGTH_LONG).show();
}
});
params.setAsJsonContent(true);
params.setBodyContent(json);
这两句是关键
后台部分代码
@RequestMapping("/add")
@ResponseBody
public List<User> add(@RequestBody User user){
userService.add(user);
return userService.findAll();
};