二次封装的功能
1,网络的get请求
2,xml数据解析
3,网络的post请求
4,支持请求头的添加
5,支持post文件上传,类似于表单
注意事项
post请求上传文件,因为不知道文件的具体的个数
1,需要先获取一个泛型为<String,File>的Map集合
2,在okHttpUtils中间,遍历集合,并添加文件
代码的实现
package com.example.guixin.oschina.base;
import com.example.guixin.oschina.util.XmlUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.PostFormBuilder;
import com.zhy.http.okhttp.callback.StringCallback;
import java.io.File;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import okhttp3.Call;
/**
* Author: 归零
* Date: 2017/3/4 9:31
* Email: 4994766@qq.com
* Description:1,get请求 2,xml数据的解析 3,post请求 4,请求头的添加
* <p>
* 需要改变的地方
* 1,NewsPagerProtocol.Callback 改为 BaseProtocol
* 2,参数不确定,提供一个方法 子类必须实现
* 3,Url不确定,提供一个方法,子类必须实现
* 4,泛型的改变
* 1,将要改变的类型,变为泛型
* 1,使用泛型后要在类或者接口中声明泛型
* 2,在类或者接口使用的地址,添加泛型
* 3,在使用的类或者接口声明泛型
* 2,
*/
public abstract class BaseProtocol<RESTYPE> {
protected int mCurrentPageIndex;
public void setCurrentPageIndex(int currentPageIndex) {
mCurrentPageIndex = currentPageIndex;
}
public void loadGetData(final BaseProtocol.Callback<RESTYPE> callback) {
String baseUrl = getUrl();
OkHttpUtils
.get()
.url(baseUrl)
.headers(getHeaderMap())
.params(getParamsMap())
.build()
.execute(new StringCallback() {
@Override
public void onError(Call call, Exception e, int id) {
callback.onError(call, e, id);
}
@Override
public void onResponse(String response, int id) {
Type type = BaseProtocol.this.getClass().getGenericSuperclass();
ParameterizedType parameterizedType = (ParameterizedType) type;
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
Type typeArgument = actualTypeArguments[0];
RESTYPE restype = (RESTYPE) XmlUtils.toBean((Class) typeArgument, response.getBytes());
callback.onResponse(restype, id);
}
});
}
public void loadPostData(final BaseProtocol.Callback<RESTYPE> callback) {
String newsUrl = getUrl();
PostFormBuilder postFormBuilder = OkHttpUtils
.post()
.url(newsUrl);
Map<String, File> postFiles = getFilesMap();
if(postFiles !=null) {
for (Map.Entry<String, File> info : postFiles.entrySet()) {
postFormBuilder.addFile(info.getKey(), info.getValue().getName(), info.getValue());
}
}
postFormBuilder.headers(getHeaderMap())
.params(getParamsMap())
.build()
.execute(new StringCallback() {
@Override
public void onError(Call call, Exception e, int id) {
callback.onError(call, e, id);
}
@Override
public void onResponse(String response, int id) {
Type type = BaseProtocol.this.getClass().getGenericSuperclass();
ParameterizedType parameterizedType = (ParameterizedType) type;
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
Type typeArgument = actualTypeArguments[0];
RESTYPE restype = (RESTYPE) XmlUtils.toBean((Class) typeArgument, response.getBytes());
callback.onResponse(restype, id);
}
});
}
/**
* 决定请求的url
* 在BaseProtocol中不知道具体的实现 交给子类
* 子类必须实现,定义成为抽象方法
*
* @return
*/
protected abstract String getUrl();
/**
* 决定请求的参数
* 在BaseProtocol中不知道具体的实现 交给子类实现
* 子类必须实现 定义成抽象方法
*
* @return
*/
protected HashMap<String, String> getParamsMap() {
return null;
}
;
/**
* 决定请求头参数
* 在BaseProtocol中不知道具体的实现 交给子类实现
* 子类选择性实现
*
* @return
*/
protected Map<String, String> getHeaderMap() {
return null;
}
/**
* post实现上传文件
* 在BaseProtocol中不知道具体的实现 交给子类实现
* 子类选择性实现
*
* @return
*/
protected Map<String, File> getFilesMap() {
return null;
}
public interface Callback<RESTYPE> {
void onError(Call call, Exception e, int id);
void onResponse(RESTYPE restype, int id);
}
}