在循环中添加 post 个参数 - OkHTTP
Add post parameters in loop - OkHTTP
我正在使用 OkHTTP 向我的服务器发出 post 请求。我知道我可以构建这样的请求:
RequestBody formBody = new FormEncodingBuilder()
.add("param1", param1)
.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
所以我想做的是动态添加参数。例如:
RequestBody formBody = new FormEncodingBuilder()
for (ParamsArray m : requestParams) {
formBody.add("param1", requestParams.value);
}
但是 RequestBody
没有函数 add
,我不知道是否可以将 FormEncodingBuilder
转换为 RequestBody
。
谢谢!
A FormEncodingBuilder
将在您构建时变成 RequestBody
。查看文档,类似这样的东西应该可以工作。
FormEncodingBuilder formBodyBuilder = new FormEncodingBuilder()
for (ParamsArray m : requestParams) {
formBodyBuilder.add("param1", requestParams.value);
}
RequestBody body = formBodyBuilder.build()
文档可在此处获取:
https://square.github.io/okhttp/2.x/okhttp/com/squareup/okhttp/FormEncodingBuilder.html
试试这个
FormEncodingBuilder formBodyBuilder = new FormEncodingBuilder();
for (ParamsArray m : requestParams) {
formBodyBuilder.add("param1", requestParams.value);
}
RequestBody formBody = formBodyBuilder.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
从 3.0.0 开始,FormEncodingBuilder is gone:
Form and Multipart bodies are now modeled. We've replaced the opaque
FormEncodingBuilder with the more powerful FormBody and
FormBody.Builder combo. Similarly we've upgraded MultipartBuilder into
MultipartBody, MultipartBody.Part, and MultipartBody.Builder.
因此,将这些版本替换为 FormBody.Builder。
代替 FormEncodingBuilder
使用
Builder paramBuilder = new FormBody.Builder();
paramBuilder.add("param1","value1");
paramBuilder.add("param2","value2");
RequestBody requestBody = paramBuilder.build();
我正在使用 OkHTTP 向我的服务器发出 post 请求。我知道我可以构建这样的请求:
RequestBody formBody = new FormEncodingBuilder()
.add("param1", param1)
.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
所以我想做的是动态添加参数。例如:
RequestBody formBody = new FormEncodingBuilder()
for (ParamsArray m : requestParams) {
formBody.add("param1", requestParams.value);
}
但是 RequestBody
没有函数 add
,我不知道是否可以将 FormEncodingBuilder
转换为 RequestBody
。
谢谢!
A FormEncodingBuilder
将在您构建时变成 RequestBody
。查看文档,类似这样的东西应该可以工作。
FormEncodingBuilder formBodyBuilder = new FormEncodingBuilder()
for (ParamsArray m : requestParams) {
formBodyBuilder.add("param1", requestParams.value);
}
RequestBody body = formBodyBuilder.build()
文档可在此处获取: https://square.github.io/okhttp/2.x/okhttp/com/squareup/okhttp/FormEncodingBuilder.html
试试这个
FormEncodingBuilder formBodyBuilder = new FormEncodingBuilder();
for (ParamsArray m : requestParams) {
formBodyBuilder.add("param1", requestParams.value);
}
RequestBody formBody = formBodyBuilder.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
从 3.0.0 开始,FormEncodingBuilder is gone:
Form and Multipart bodies are now modeled. We've replaced the opaque FormEncodingBuilder with the more powerful FormBody and FormBody.Builder combo. Similarly we've upgraded MultipartBuilder into MultipartBody, MultipartBody.Part, and MultipartBody.Builder.
因此,将这些版本替换为 FormBody.Builder。
代替 FormEncodingBuilder
使用
Builder paramBuilder = new FormBody.Builder();
paramBuilder.add("param1","value1");
paramBuilder.add("param2","value2");
RequestBody requestBody = paramBuilder.build();