How to use OKHTTP to make a post request?

后端 未结 13 1054
佛祖请我去吃肉
佛祖请我去吃肉 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:32

    1. Add the following to the build.gradle

    compile 'com.squareup.okhttp3:okhttp:3.7.0'

    1. Create a new thread, in the new thread add the following code.
    OkHttpClient client = new OkHttpClient();
    MediaType MIMEType= MediaType.parse("application/json; charset=utf-8");
    RequestBody requestBody = RequestBody.create (MIMEType,"{}");
    Request request = new Request.Builder().url(url).post(requestBody).build();
    Response response = client.newCall(request).execute();
    

提交回复
热议问题