问题描述
我正在使用OKHTTP 3.x版本.我想发布多个参数,并想在循环中添加参数.我知道在2.x版中,我可以使用FormEncodingBuilder并向其循环添加参数,然后从中创建一个请求主体.但是在3.x中,该类已被删除.
I am using OKHTTP 3.x version. I want to post multiple parameters and would like to add the params in a loop.I know that in version 2.x , I can use FormEncodingBuilder and add params to it in loop and then from it create a request body.But In 3.x , the class has been removed.
这是我当前的代码:
RequestBody formBody = new FormBody.Builder()
.add("Param1", value1)
.add("Param2", value2)
.build();
Request request = new Request.Builder()
.url("url")
.post(formBody)
.build();
现在我想在循环中添加5个参数,即通过在循环中构建formbody来创建请求主体.就像我在上面写的一样,我知道如何在OKHTTP 2.x版中执行此操作,但是我正在使用3.x版.
Now I want to add 5 params but in a loop i.e create request body by building formbody in a loop.Like I wrote above, I know how to do it in OKHTTP version 2.x but I am using version 3.x.
感谢任何帮助或指导.
预先感谢
推荐答案
这是我的方法:
FormBody.Builder formBuilder = new FormBody.Builder()
.add("key", "123");
// dynamically add more parameter like this:
formBuilder.add("phone", "000000");
RequestBody formBody = formBuilder.build();
Request request = new Request.Builder()
.url("https://aaa.com")
.post(formBody)
.build();
这篇关于如何在Android中的OKHTTP 3.x中动态发送(或循环发送)发布参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!