okhttp的辅助类
-
Android Studio
使用前,对于Android Studio的用户,可以选择添加:
compile 'com.squareup.okhttp:okhttp:2.4.0' compile 'com.squareup.okio:okio:1.5.0'
-
Eclispe
Eclipse的用户,可以下载jar okhttp JAR和 okio JAR添加依赖就可以用了。
注意
由于整合了Gson,支持直接返回对象(例如User
),对象集合(例如:List<User>
),所以记得使用时必须加入Gson的依赖,jar包gson-2.2.1.jar.
sample项目的代码也上传了,大家可以下载参考里面的依赖,以及示例用法。ps:以module的形式导入。
最后将OkHttpClientManager拷贝到项目即可。
##目前支持
- 一般的get请求
- 一般的post请求
- 基于Http的文件上传
- 文件下载
- 加载图片
- 支持请求回调,直接返回对象、对象集合
- 支持session的保持
- 支持自签名网站https的访问,提供方法设置下证书就行
##用法示例
OkHttpClientManager.getAsyn("http://192.168.56.1:8080/okHttpServer/user!getSimpleString", new OkHttpClientManager.ResultCallback<String>()
{
@Override
public void onError(Request request, Exception e)
{
e.printStackTrace();
}
@Override
public void onResponse(String u)
{
mTv.setText(u);
}
});
OkHttpClientManager.getAsyn("http://192.168.56.1:8080/okHttpServer/user!getUser",
new OkHttpClientManager.ResultCallback<User>()
{
@Override
public void onError(Request request, Exception e)
{
e.printStackTrace();
}
@Override
public void onResponse(User u)
{
mTv.setText(u.toString());
}
});
注意会根据传入的泛型,比如User,则会将服务器返回的Json字符串转化为user对象,如果在转化过程中发生错误,onError会被回调。记得加入Gson的依赖。
OkHttpClientManager.getAsyn("http://192.168.56.1:8080/okHttpServer/user!getUsers",
new OkHttpClientManager.ResultCallback<List<User>>()
{
@Override
public void onError(Request request, Exception e)
{
e.printStackTrace();
}
@Override
public void onResponse(List<User> us)
{
Log.e("TAG", us.size() + "");
mTv.setText(us.get(1).toString());
}
});
注意会根据传入的泛型,比如List,则会将服务器返回的Json字符串转化为List对象,如果在转化过程中发生错误,onError会被回调。记得加入Gson的依赖。
OkHttpClientManager.postAsyn(url,callback,params);
File file = new File(Environment.getExternalStorageDirectory(), "test1.txt");
if (!file.exists())
{
return;
}
OkHttpClientManager.getUploadDelegate().postAsyn("http://192.168.1.103:8080/okHttpServer/fileUpload",//
"mFile",//
file,//
new OkHttpClientManager.Param[]{
new OkHttpClientManager.Param("username", "zhy"),
new OkHttpClientManager.Param("password", "123")},//
new OkHttpClientManager.ResultCallback<String>()
{
@Override
public void onError(Request request, Exception e)
{
e.printStackTrace();
}
@Override
public void onResponse(String filePath)
{
Log.e("TAG", filePath);
}
}
);
OkHttpClientManager.getDisplayImageDelegate()
.displayImage(mImageView,
"http://images.csdn.net/20150817/1.jpg");
会自动根据ImageView的大小进行压缩。
OkHttpClientManager.getDownloadDelegate().downloadAsyn(
"url",
Environment.getExternalStorageDirectory().getAbsolutePath(),
new OkHttpClientManager.ResultCallback<String>()
{
@Override
public void onError(Request request, Exception e)
{
}
@Override
public void onResponse(String response)
{
Toast.makeText(MainActivity.this, response + "下载成功", Toast.LENGTH_SHORT).show();
}
});
非常简单,拿到xxx.cert的证书。
然后调用
OkHttpClientManager.getInstance()
.setCertificates(inputstream);
建议使用方式,例如我的证书放在assets目录:
/**
* Created by zhy on 15/8/25.
*/
public class MyApplication extends Application
{
@Override
public void onCreate()
{
super.onCreate();
try
{
OkHttpClientManager.getInstance()
.setCertificates(getAssets().open("aaa.cer"),
getAssets().open("server.cer"));
} catch (IOException e)
{
e.printStackTrace();
}
}
}
即可。别忘了注册Application。
ResultCallback包含两个回调,onBefore
和onAfter
。两个方法都在UI线程回调,一个在请求开始前,一个是请求结束。所以你可以在onBefore
弹出等待框等操作,onAfter
隐藏等待框等。
OkHttpClientManager.getAsyn("http://192.168.56.1:8080/okHttpServer/user!getUser",
new OkHttpClientManager.ResultCallback<User>()
{
@Override
public void onBefore(Request request, Exception e)
{
showWaitingDialog();
}
@Override
public void onAfter(Request request, Exception e)
{
dismissWaitingDialog();
}
@Override
public void onError(Request request, Exception e)
{
e.printStackTrace();
}
@Override
public void onResponse(User u)
{
mTv.setText(u.toString());
}
});
如果你的项目所有的框是一致的,或者可以分类,你可以按照如下方式编写几个模板:
public abstract class MyResultCallback<T> extends ResultCallback<T>
{
@Override
public void onBefore()
{
super.onBefore();
//显示等待框等
setTitle("loading...");
}
@Override
public void onAfter()
{
super.onAfter();
//隐藏等待框等
setTitle("Sample-okHttp");
}
}
目前比较常见的API可以直接通过OkHttpClientManager.methodName访问,当然有很多不常用的方法,会被封装在对应的模块内部,大体分为以下几个模块:
- HttpsDelegate
- DownloadDelegate
- DisplayImageDelegate
- GetDelegate
- UploadDelegate
- PostDelegate
如果你有比较特殊的需求,不放通过OkHttpClientManager.getXXXDelegate().methodName去访问。
比如加载图片:
OkHttpClientManager.getDisplayImageDelegate().displayImage();
比如get请求,直接将文件作为请求体:
OkHttpClientManager.getPostDelegate().post(url,file,callback);