xUtils3 post方法传json参数

    xiaoxiao2021-03-25  100

    今天写程序的时候遇到了这个问题 记录一下

    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(); };
    转载请注明原文地址: https://ju.6miu.com/read-18410.html

    最新回复(0)