接口
1. 正常接口
代码
public interface MyOkHttpListener {
void OnOk(String json);
void OnNoOk(String message);
}
2. 进度接口
代码
public interface MyProgressListener {
void Error(String message);
void Finish();
void GetProgress(int progress);
}
OkHttp的分装
单例分装
代码
public class OkHttpUtil {
private OkHttpClient client = null;
private OkHttpUtil(){
Interceptor interceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request().newBuilder().header("token", "hasdkdhkasjhdka").build();
return chain.proceed(request);
}
};
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
client = new OkHttpClient.Builder()
.callTimeout(1, TimeUnit.MINUTES)
.readTimeout(1,TimeUnit.MINUTES)
.addInterceptor(interceptor)
.build();
}
private static OkHttpUtil okHttpUtil = null;
public static OkHttpUtil GetValue(){
if (okHttpUtil == null){
synchronized (Object.class){
if (okHttpUtil == null){
okHttpUtil = new OkHttpUtil();
}
}
}
return okHttpUtil;
}
public void UpGet(String url , final MyOkHttpListener myOkHttpListener){
Request request = new Request.Builder()
.get()
.url(url)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
myOkHttpListener.OnNoOk(e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
myOkHttpListener.OnOk(response.body().string());
}
});
}
public void UpPut(String url , Map<String,String> map , final MyOkHttpListener myOkHttpListener){
FormBody.Builder builder = new FormBody.Builder();
Set<Map.Entry<String, String>> entries = map.entrySet();
for (Map.Entry<String, String> entry : entries) {
String key = entry.getKey();
String value = entry.getValue();
builder.add(key,value);
}
FormBody formBody = builder.build();
Request request = new Request.Builder()
.post(formBody)
.url(url)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
myOkHttpListener.OnNoOk(e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
myOkHttpListener.OnOk(response.body().string());
}
});
}
public void UpDown(String url , final String FileName , final MyProgressListener myProgressListener){
Request request = new Request.Builder()
.get()
.url(url)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
myProgressListener.Error(e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
long max = response.body().contentLength();
InputStream inputStream = response.body().byteStream();
FileOutputStream fileOutputStream = new FileOutputStream(FileName);
int len = 0;
int count = 0;
byte[] bytes = new byte[1024];
while((len = inputStream.read(bytes)) != -1){
count += len;
fileOutputStream.write(bytes,0,len);
myProgressListener.GetProgress((int) (count*100/max));
}
myProgressListener.Finish();
}
});
}
public void UpDownload(String url , String FileName , String type , String path , final MyOkHttpListener myOkHttpListener){
MultipartBody multipartBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", FileName, RequestBody.create(MediaType.parse(type), path))
.build();
Request request = new Request.Builder()
.post(multipartBody)
.url(url)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
myOkHttpListener.OnNoOk(e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
myOkHttpListener.OnOk("上传成功");
}
});
}
}
MVC模式使用OkHttp
接口
添加自己要用的OkHttp方法 以及参数
public interface ModelListener {
void DoPost(String url , Map<String,String> map ,MyOkHttpListener myOkHttpListener);
}
MVC类
实现你的接口并且添加工具类的方法
public class MyModel implements modelListener {
@Override
public void getGet(String url, MyOkHttpIm myOkHttpIm) {
OkHttpUtil.getInstance().DoGet(url,myOkHttpIm);
}
}
MVC调用
实例化Model方法 调用
Map<String,String> map = new HashMap<>();
map.put("itemid", "2");
map.put("act", "ad_app");
myModel.DoPost("http://api.yunzhancn.cn/api/app.interface.php?siteid=78703&", map, new MyOkHttpListener() {
@Override
public void OnOk(String json) {
Message obtain = Message.obtain();
obtain.what = 2;
obtain.obj = json;
handler.sendMessage(obtain);
}
@Override
public void OnNoOk(String message) {
Message obtain = Message.obtain();
obtain.what = 1;
obtain.obj = message;
handler.sendMessage(obtain);
}
});