RequestBody formBody=builder.build();
Request request=new Request.Builder().url(netUrl).post(formBody).build();
Call call=okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
//请求失败的处理
}
@Override
public void onResponse(Call call, Response response) throws IOException {
}
});
}
所以我们还有一种方式可以装取键值对,通过ArrayList这样的数据结构。
//首先要有一个RequestParameter类
public class RequestParameter implements Serializable {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
private String name;
private String value;
public RequestParameter(String name,String value){
this.name=name;
this.value=value;
}
}
List parameter=new ArrayList<>();
RequestParameter rp1=new RequestParameter(“name”,“哈哈”);
parameter.add(rp1);
RequestParameter rp2=new RequestParameter(“client”,“Android”);
parameter.add(rp2);
//创建一个FormBody.Builder
FormBody.Builder builder=new FormBody.Builder();
if (parameter!=null&¶meter.size()>0){
for (final RequestParameter p : parameter) {
builder.add(p.getName(),p.getValue());
}
}
RequestBody formBody=builder.build();
OkHttpClient okHttpClient=new OkHttpClient();
Request request=new Request.Builder().url(netUrl).post(formBody).build();
Call call=okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
//请求失败的处理
}
@Override
public void onResponse(Call call, Response response) throws IOException {
}
});
1.3、MultipartBody—文件上传
MultipartBody可以构建与HTML文件上传格式兼容的复杂请求体。
File file=new File(Environment.
getExternalStorageDirectory(), “balabala.png”);
MediaType MEDIA_TYPE_PNG = MediaType.parse(“image/png”);
RequestBody filebody = MultipartBody.create(MEDIA_TYPE_PNG, file);
MultipartBody.Builder multiBuilder=new MultipartBody.Builder();
multiBuilder.addFormDataPart(“file”, file.getName(), filebody);
//参数以添加header方式将参数封装,否则上传参数为空
// 设置请求体
multiBuilder.setType(MultipartBody.FORM);
//这里是 封装上传图片参数
multiBuilder.addFormDataPart(“file”, file.getName(), filebody);
// 封装请求参数,这里最重要
HashMap
params.put(“client”,“Android”);
params.put(“uid”,“1061”);
params.put(“token”,“1911173227afe098143caf4d315a436d”);
params.put(“uuid”,“A000005566DA77”);
//参数以添加header方式将参数封装,否则上传参数为空
if (params != null && !params.isEmpty()) {
for (String key : params.keySet()) {
multiBuilder.addPart(
Headers.of(“Content-Disposition”, “form-data; name=”" + key + “”"),
RequestBody.create(null, params.get(key)));
}
}
RequestBody multiBody=multiBuilder.build();
OkHttpClient okHttpClient=new OkHttpClient();
Request request=new Request.Builder().url(netUrl).post(multiBody).build();
Call call=okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
();
Request request=new Request.Builder().url(netUrl).post(multiBody).build();
Call call=okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)