當使用下面的代碼時會提示 RequestBody.create() 已過時。
RequestBody rb = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json.toString());
如下圖:
解決辦法:文章來源:http://www.zghlxwxcb.cn/news/detail-600040.html
import okhttp3.MediaType.Companion.*;
import okhttp3.RequestBody.Companion.*;
//String轉(zhuǎn)RequestBody String、ByteArray、ByteString都可以用toRequestBody()
MediaType mediaType=MediaType.Companion.parse("application/json;charset=utf-8");
RequestBody stringBody=RequestBody.Companion.create("body參數(shù)", mediaType);
Request request=new Request
.Builder()
.post(stringBody)
.build();
//File轉(zhuǎn)RequestBody
MediaType mediaType=MediaType.Companion.parse("text/x-markdown; charset=utf-8");
File file=new File("");
RequestBody fileBody=RequestBody.Companion.create(file,mediaType);
Request request=new MultipartBody.Builder()
.addFormDataPart("file", file.getName(),fileBody)
.build();
如下圖:文章來源地址http://www.zghlxwxcb.cn/news/detail-600040.html
到了這里,關(guān)于在Java版的OkHttp3 中 RequestBody.create() 過時解決方案的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!