上一篇Okhttp系列简单的不用传参的Get请求示例用Intelij Idea创建java项目如下图添加阿里云镜像maven{urlhttps://maven.aliyun.com/repository/public/}添加Okhttp依赖implementationcom.squareup.okhttp3:okhttp:4.12.0java代码如下packageorg.example;importjava.io.IOException;importjava.util.Collections;importjava.util.concurrent.TimeUnit;importokhttp3.*;publicclassPostExample{publicstaticfinalMediaTypeJSONMediaType.get(application/json; charsetutf-8);finalOkHttpClientclientnewOkHttpClient();Stringpost(Stringurl,Stringjson)throwsIOException{RequestBodybodyRequestBody.create(json,JSON);RequestrequestnewRequest.Builder().url(url).post(body).build();try(Responseresponseclient.newCall(request).execute()){returnresponse.body().string();}}StringbowlingJson(Stringplayer1,Stringplayer2){return{\winCondition\:\HIGH_SCORE\,\name\:\Bowling\,\round\:4,\lastSaved\:1367702411696,\dateStarted\:1367702378785,\players\:[{\name\:\player1\,\history\:[10,8,6,7,8],\color\:-13388315,\total\:39},{\name\:\player2\,\history\:[6,10,5,10,10],\color\:-48060,\total\:41}]};}publicstaticvoidmain(String[]args)throwsIOException{PostExampleexamplenewPostExample();Stringjsonexample.bowlingJson(Jesse,Jake);Stringresponseexample.post(https://jsonplaceholder.typicode.com/posts,json);System.out.println(response);}}执行结果{ winCondition: HIGH_SCORE, name: Bowling, round: 4, lastSaved: 1367702411696, dateStarted: 1367702378785, players: [ { name: Jesse, history: [ 10, 8, 6, 7, 8 ], color: -13388315, total: 39 }, { name: Jake, history: [ 6, 10, 5, 10, 10 ], color: -48060, total: 41 } ], id: 101 }