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

后端 未结 7 1910
不思量自难忘°
不思量自难忘° 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 01:51

    You just need to format the body of the POST before creating the RequestBody object.

    You could do this manually, but I'd suggest you use the MimeCraft library from Square (makers of OkHttp).

    In this case you'd need the FormEncoding.Builder class; set the contentType to "application/x-www-form-urlencoded" and use add(name, value) for each key-value pair.

提交回复
热议问题