OKHttp3请求http接口传参body

/**
     * 推送body数据,post接口
     * @param url 
     * @param params
     * @param token
     * @return
     */
    public static JSONObject doBodyPost(String url, JSONObject params,String token) {
          
   
        try {
          
   
            //设置 http头 Content-Type 的值
            MediaType JSON = MediaType.parse("application/json; charset=utf-8");
            RequestBody body =  RequestBody.create(params.toString(),JSON);
            Request request = new Request.Builder()
                    .header("X-66nao-Auth-T",token)
                    .post(body)
                    .url(url).build();
            Response response = OkHttpUtil.getInstance().newCall(request).execute();
            if (response.isSuccessful()) {
          
   
                String content = response.body().string();
                //System.out.println("response:"+content);
                if (ObjectUtil.isNotEmpty(content)) {
          
   
                    JSONObject jsonObject = JSONObject.parseObject(content);
                    return jsonObject;
                }
            }
        } catch (Exception e) {
          
   
            e.printStackTrace();
        }
        return null;
    }
经验分享 程序员 微信小程序 职场和发展