How to add parameters to api (http post) using okhttp library in Android

后端 未结 7 1908
不思量自难忘°
不思量自难忘° 2020-11-30 01:33

In my Android application, I am using okHttp library. How can I send parameters to the server(api) using the okhttp library? currently I am using the following code to acces

7条回答
  •  自闭症患者
    2020-11-30 02:02

    For OkHttp 3.x, FormEncodingBuilder was removed, use FormBody.Builder instead

            RequestBody formBody = new FormBody.Builder()
                    .add("email", "Jurassic@Park.com")
                    .add("tel", "90301171XX")
                    .build();
    
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url(url)
                    .post(formBody)
                    .build();
    
            Response response = client.newCall(request).execute();
            return response.body().string();
    

提交回复
热议问题