How to use OKHTTP to make a post request?

后端 未结 13 1067
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-02 05:06

I read some examples which are posting jsons to the server.

some one says :

OkHttp is an implementation of the HttpUrlConnection interface p

13条回答
  •  生来不讨喜
    2020-12-02 05:41

    The current accepted answer is out of date. Now if you want to create a post request and add parameters to it you should user MultipartBody.Builder as Mime Craft now is deprecated.

    RequestBody requestBody = new MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("somParam", "someValue")
            .build();
    
    Request request = new Request.Builder()
            .url(BASE_URL + route)
            .post(requestBody)
            .build();
    

提交回复
热议问题