当前位置:首页>开发>正文

xUtils怎么post请求上传json数据 如何使用restclient来发送post请求参数

2023-04-14 05:17:05 互联网 未知 开发

 xUtils怎么post请求上传json数据 如何使用restclient来发送post请求参数

xUtils怎么post请求上传json数据

RequestParams requestParams =new RequestParams("http://" SpeechApp.url "/interfaceAndroid/Project/AddServiceItemQuote")
ArrayListarrayList =new ArrayList<>() arrayList.add(new Dingdan_Model("1","2","3","4","5")) arrayList.add(new Dingdan_Model("2","2","3","4","5")) arrayList.add(new Dingdan_Model("3","2","3","4","5")) arrayList.add(new Dingdan_Model("4","2","3","4","5")) arrayList.add(new Dingdan_Model("5","2","3","4","5")) User user=new User(arrayList,"12345") Gson g=new Gson() String a=g.toJson(user) requestParams.setAsJsonContent(true) requestParams.setBodyContent(a) requestParams.setCharset("utf-8") x.http().post(requestParams, new Callback.CommonCallback() { @Override public void onSuccess(String result) { System.out.println("result==" result) Toast.makeText(Wode_wenti.this,result,Toast.LENGTH_LONG).show() } @Override public void onError(Throwable ex, boolean isOnCallback) { System.out.println(ex.toString()) Toast.makeText(Wode_wenti.this,ex.toString(),Toast.LENGTH_LONG).show() if (ex instanceof HttpException) { // 网络错误 HttpException httpEx = (HttpException) ex int responseCode = httpEx.getCode() String responseMsg = httpEx.getMessage() String errorResult = httpEx.getResult() System.out.println("responseCode==" responseCode) System.out.println("responseMsg==" responseMsg) System.out.println("errorResult==" errorResult) } } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } })

如何使用restclient来发送post请求参数

我喜欢使用 restclient 来测试我的 REST 风格的应用程序。一般我就是用GET方法,今天用到了POST方法。POST传递参数应该放在body里面,对长度没有限制。不像GET对URL的限制是1024字节。

  运行 restclient ,点选Method选项卡的“POST”方法。然后选择Body选项卡,下下拉列表中选择”String body“的选项,配置上 application/x-www-form-urlencodedcharset=UTF-8 。再出现的body里面写入字符串,也就是你的请求条件,如:query=xpsF

  这样就可以传递post的参数了。

  java代码如下:springmvc写的
@RequestMapping(value = "/test", method = { RequestMethod.GET,
RequestMethod.POST })
public void test(HttpServletResponse response, @RequestBody String message) {
//注意这里的:@RequestBody String message
LOGGER.debug(String.format("receive message %s", message))
Mapmap = Maps.newHashMap() try { map.put("result", message) Tools.printToJson(JSON.toJSONString(map), response) } catch (Exception e) { LOGGER.error(e.getMessage(), e) } } 转载 kanpiaoxue