//使用Gson将对象转换为json字符串 String json = gson.toJson(book); //MediaType 设置Content-Type 标头中包含的媒体类型值 RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8") , json); Request request = new Request.Builder() .url("http://172.20.192.168...
StringpostBodyJson="{\"name\":\"tony\",\"age\":18}"; // 构造参数 RequestBodyrequestBody=RequestBody.create(postBodyJson, JSON); Requestrequest=newRequest.Builder().url(url).header("token","123abc").post(requestBody).build(); OkHttpClientokHttpClient=newOkHttpClient(); Callcall=okHttp...
public static final MediaType JSON = MediaType.get("application/json; charset=utf-8"); final OkHttpClient client = new OkHttpClient(); String post(String url, String json) throws IOException { RequestBody body = RequestBody.create(json, JSON); Request request = new Request.Builder() .url...
post请求创建request和get是一样的,只是post请求需要提交一个表单,就是RequestBody。表单的格式有好多种,普通的表单是: RequestBody body = new FormBody.Builder() .add("键", "值") .add("键", "值") ... .build(); RequestBody的数据格式都要指定Content-Type,常见的有三种: application/x-www-form...
.post(body) .build(); Response response; try { response = new OkHttpClient().newCall(requestOk).execute(); String jsonString = response.body().string(); if(response.isSuccessful()){ return jsonString; } } catch (Exception e) { ...
builder = new Request.Builder() .url(url) .post(requestBody); if (headers != null ...
/*** HTTP接口-POST方式,请求参数形式为body-json形式** @param url* @param jsonString* @return String*/public static String sendPostWithJson(String url, String jsonString) throws IOException {OkHttpClient client = new OkHttpClient().newBuilder().build();RequestBody body = RequestBody.create(...
* 初始化post方法 * * @param isJsonPost true等于json的方式提交数据,类似postman里post方法的raw * false等于普通的表单提交 * @return */ public OkHttpUtils post(boolean isJsonPost) { RequestBody requestBody; if (isJsonPost) { String json = ""; ...
System.out.println(response.body().string()); } } }); post 请求 -- json格式提交 OkHttpClientokHttpClient=newOkHttpClient.Builder() .connectTimeout(10, TimeUnit.SECONDS) .writeTimeout(10, TimeUnit.SECONDS) .readTimeout(20, TimeUnit.SECONDS) ...
();json.put(key,value);}RequestBody requestBody=RequestBody.create(JSON,json.toString());Request request=newRequest.Builder().url(url).tag(tag).post(requestBody).build();Call call=this.mClient.newCall(request);call.enqueue(newCallback(){publicvoidonResponse(Call call,finalResponse response)...