一、发送post请求,带body
适用再次调用别的post接口,并且传参
InAndOutRemarkInfo inAndOutRemarkInfo = new InAndOutRemarkInfo(); Gson gson = new GsonBuilder().create(); String jsonRequestBody = gson.toJson(inAndOutRemarkInfo); OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.get("application/json; charset=utf-8"); RequestBody body = RequestBody.create(mediaType, jsonRequestBody); //调新增接口 Request request = new Request.Builder() .url(remarkUrl) .post(body) .build(); Response response = client.newCall(request).execute(); responseBody = response.body().string(); return responseBody;
二、发送put请求,带body
适用再次调用别的put接口,并且传参
List<InAndOutRemarkInfo> inAndOutRemarkInfoList = new ArrayList<>(); InAndOutRemarkInfo inAndOutRemarkInfo = new InAndOutRemarkInfo(); String responseBody = null; if (org.apache.commons.lang3.StringUtils.isNotBlank(remarkInfo)){ //新增备注数据 inAndOutRemarkInfo.setRemark(remarkInfo); //给进厂标识,本来需要判断,此处先给个数据-进厂 inAndOutRemarkInfo.setInAndOutFactory("进厂"); inAndOutRemarkInfo.setDate(date); inAndOutRemarkInfo.setMaterialName(materialName); inAndOutRemarkInfo.setMntDate(new Date()); String userCode = request.getHeader("usercode"); inAndOutRemarkInfo.setUserCode(userCode); inAndOutRemarkInfo.setUserName(userCode); inAndOutRemarkInfoList.add(inAndOutRemarkInfo); //构建修改数据Collectionjson String collectionJson = RestfulTool.buildCollection(inAndOutRemarkInfoList); OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.get("application/json; charset=utf-8"); RequestBody body = RequestBody.create(mediaType, collectionJson); Request request = new Request.Builder() .url(remarkUrl) .put(body) .build(); Response response = client.newCall(request).execute(); responseBody = response.body().string(); }else { throw new BusinessException("","","备注信息为空"); }