1、想得到的结果更加简洁,想得到请求的对象,比如不想解析json、xml等,提出这样的需要,在网络框架中应该如何拓展?实际上,如果不想改变网络框架的话,最好在上层封装接口
想通过对这个泛型进行处理,得到我们所需要的结果
2、定义接口
代码:
public interface Convert {
Object parse(HttpResponse response, Type type) throws IOException;
boolean isCanParse(String contentType);
Object parse(String content, Type type) throws IOException;
}
3、对接口进行拓展
通过Gson返回所需要的对象
代码:
public class JsonConvert implements Convert {
private Gson gson = new Gson();
private static final String CONTENT_TYPE = "application/json;charset=UTF-8";
@Override
public Object parse(HttpResponse response, Type type) throws IOException {
Reader reader = new InputStreamReader(response.getBody());
return gson.fromJson(reader, type);
}
@Override
public boolean isCanParse(String contentType) {
return CONTENT_TYPE.equals(contentType);
}
@Override
public Object parse(String content, Type type) throws IOException {
return gson.fromJson(content, type);
}
}
4、上层封装完成以后,考虑一下如何与下面的接口进行接入
将上面的接口改成抽象类,否则在对泛型进行修改和获取的时候是一件麻烦的事情
public class MoocRequest {
private String mUrl;
private HttpMethod mMethod;
private byte[] mData;
private MoocResponse mResponse;
private String mContentType;
public String getUrl() {
return mUrl;
}
public void setUrl(String url) {
mUrl = url;
}
public HttpMethod getMethod() {
return mMethod;
}
public void setMethod(HttpMethod method) {
mMethod = method;
}
public byte[] getData() {
return mData;
}
public void setData(byte[] data) {
mData = data;
}
public MoocResponse getResponse() {
return mResponse;
}
public void setResponse(MoocResponse response) {
mResponse = response;
}
public String getContentType() {
return mContentType;
}
public void setContentType(String contentType) {
mContentType = contentType;
}
}
5、对MoocResponse进行封装,原因是拿到它的回调。
原来它是需要转换成一个string类型,但是我们需要将它转换成一个泛型。
如果接口改变的比较多的话,需要在workstation中进行修改相应的接口,但是实际上需要尽可能少地去改变原来的框架,可以对MoocResponse进行封装
6、对MoocResponse进行封装
7、外界调用
package com.imooc.service;
import com.imooc.http.HttpMethod;
import com.imooc.service.convert.Convert;
import com.imooc.service.convert.JsonConvert;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @author nate
*/
public class MoocApiProvider {
private static final String ENCODING = "utf-8";
private static WorkStation sWorkStation = new WorkStation();
private static final List<Convert> sConverts = new ArrayList<>();
static {
sConverts.add(new JsonConvert());
}
public static byte[] encodeParam(Map<String, String> value) {
if (value == null || value.size() == 0) {
return null;
}
StringBuffer buffer = new StringBuffer();
int count = 0;
try {
for (Map.Entry<String, String> entry : value.entrySet()) {
buffer.append(URLEncoder.encode(entry.getKey(), ENCODING)).append("=").
append(URLEncoder.encode(entry.getValue(), ENCODING));
if (count != value.size() - 1) {
buffer.append("&");
}
count++;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return buffer.toString().getBytes();
}
public static void helloWorld(String ul, Map<String, String> value, MoocResponse response) {
MoocRequest request = new MoocRequest();
WrapperResponse wrapperResponse = new WrapperResponse(response, sConverts);
request.setUrl(ul);
request.setMethod(HttpMethod.POST);
request.setData(encodeParam(value));
request.setResponse(wrapperResponse);
sWorkStation.add(request);
}
}