val postRequest = Request.Builder() .url("http://192.168.1.151:8080/post") .post(requestBody) .build()
图片上传:
1 2 3 4 5 6 7 8
val file=File("/storage/emulated/0/Download/Screenshot_1614074735.png") val fileBody=file.asRequestBody() val filePart=MultipartBody.Builder().addFormDataPart(body = fileBody,name = "image",filename ="test.png").build()
val fileRequest = Request.Builder() .url("http://192.168.1.151:8080/file")//请求地址 .post(filePart) .build()
funuploadFileAndParams(url: String,filePath: String,paramsMap:HashMap<String,String>,callback: HttpCallback){ val builder=MultipartBody.Builder().setType(MultipartBody.FORM) val file = File(filePath)
if (!file.exists() || file.isDirectory) { throw RuntimeException("the file isn't exit or is directory") }
val fileBody = file.asRequestBody() builder.addFormDataPart(body = fileBody, name = "image", filename = "test.png").build()
for (entry in paramsMap){ builder.addFormDataPart(entry.key,entry.value) } val fileRequest = Request.Builder() .url("${baseUrl}${url}")//请求地址 .post(builder.build()) .build() realRequest(fileRequest,callback) }