基于okhttp封装网络库 (2)
前言:上篇只是封装了一个基础的抽象类,假设我们需要发送一个 json 请求,那么该如何对 BaseRequest 扩展呢?
第一步:确定 MediaType 并解析
//该函数在 okhttp3.MediaType 包下
public static final MediaType Media_Json = MediaType.parse("application/json; charset=utf-8");
//BaseRequest 中的抽象方法
public MediaType getMediaType() {
return Media_Json;
}
第二步,将 bodyMap 解析成 Json格式
protected String getRequestString() {
if (mBodyMap != null && mBodyMap.size() > 0) {
try {
// fastjson 这个库已经为我们封装好将 bodymap 解析成 json的 api,我们直接调用即可。
String json = JSON.toJSONString(mBodyMap, SerializerFeature.WriteNullStringAsEmpty);
return json.toString();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
结果:JsonRequest
public abstract class JSONRequest<T> extends BaseRequest<T> {
public static final MediaType Media_Json = MediaType.parse("application/json; charset=utf-8");
public JSONRequest( ) {
super( );
}
public MediaType getMediaType() {
return Media_Json;
}
protected String getRequestString() {
if (mBodyMap != null && mBodyMap.size() > 0) {
try {
String json = JSON.toJSONString(mBodyMap, SerializerFeature.WriteNullStringAsEmpty);
return json.toString();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
}
上面只是针对 POST 请求方式的 主体内容格式的封装,下面才真正开始写一个能供业务层调用的请求类 JsonReQuestTask 。思路也很简单,就是完成基础抽象类抽象方法即可,原理在上篇的开篇讲到。
public class JsonReQuestTask extends JSONRequest {
public BookDetailTask() {
super();
}
@Override
public String getApi() {
return "/api";
}
@Override
public int getHttpMethod() {
return HttpMethod.POST;
}
@Override
public Class getModelClass() {
return Bean.class;
}
扩展:表单请求 FormRequest
public abstract class FormRequest<T> extends BaseRequest<T> {
public static final MediaType Media_FORM = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");
public FormRequest() {
super();
}
public MediaType getMediaType() {
return Media_FORM;
}
protected String getRequestString() {
if (mBodyMap != null && mBodyMap.size() > 0) {
try {
String json = encodeParameters(mBodyMap, "utf-8");
return json.toString();
} catch (Exception e) {
e.printStackTrace();
}
}
return "";
}
}
扩展:文件上传 MultiPartRequest
public abstract class MultiPartRequest<T> extends FormRequest<T> {
public static final MediaType Media_PART = MediaType.parse("multipart/form-data; charset=utf-8");
protected List<File> mFiles = new ArrayList<File>();
public MultiPartRequest() {
super();
}
public MediaType getMediaType() {
return Media_PART;
}
protected Request buildRequest() {
RequestBody body = null;
Request.Builder builder = new Request.Builder();
switch (getHttpMethod()) {
case HttpMethod.GET:
//TODO 抛出异常,一般不用 GET做上传。
break;
case HttpMethod.POST:
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM);
for (File file : mFiles) {
if (file.exists()) {
RequestBody requestBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
bodyBuilder.addFormDataPart("file", file.getName(), requestBody);
}
}
for (Map.Entry<String, String> entry : mBodyMap.entrySet()) {
if (TextUtils.isEmpty(entry.getKey()) || TextUtils.isEmpty(entry.getValue())) {
continue;
}
bodyBuilder.addFormDataPart(entry.getKey(), entry.getValue());
}
body = bodyBuilder.build();
builder.url(getUrl()).post(body);
break;
default:
//TODO
break;
}
return builder.build();
}
}