1、POST请求
========
主要来讲一下post请求,post方式中的Request需要传递一个RequestBody作为post的参数。RequestBody有两个子类:FormBody和MultipartBody
1.1、RequestBody–json数据提交
public static final MediaType JSON = MediaType.parse(“application/json; charset=utf-8”);
OkHttpClient client = new OkHttpClient();
String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
//同步
Response response = client.newCall(request).execute();
f (response.isSuccessful()) {
return response.body().string();
} else {
throw new IOException("Unexpected code " + response);
}
}
1.2、FromB
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
ody—表单提交 这种能满足大部分的需求
romBody用于提交表单键值对,key-value,其作用类似于HTML中的标记。比如username=“LHX”,age="21"等类似的键值对。
我们可以使用HashMap<String,String>这样的数据结构来存储接口所需参数的键值对,它的查找速度为O(1),很快,但是对于API接口参数来说,数据不会太多,查找快体现不出优势来,并且HashMap比较耗费内存。以下是使用hashmap的例子:
private void fetchDataByPost() {
//把参数传进Map中
HashMap<String,String> paramsMap=new HashMap<>();
paramsMap.put(“name”,“哈哈”);
paramsMap.put(“client”,“Android”);
paramsMap.put(“id”,“3243598”);
FormBody.Builder builder = new FormBody.Builder();
for (String key : paramsMap.keySet()) {
//追加表单信息
builder.add(key, paramsMap.get(key));
}
OkHttpClient okHttpClient=new OkHttpClient();
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);