1》Okhttp-Utils是GitHub上大神封装的,使用方便,不必像原生Okhttp要用Handle来更新UI和开启子线程网络请求,下面只贴方法 布局可以简单自己用于网络请求更新UI看效果 。
首依赖
compile files('libs/okhttputils-2_6_2.jar')
第一步:配置文件代码如下
默认情况下,将直接使用okhttp默认的配置生成OkhttpClient,如果你有任何配置,记得在Application中调用initClient
方法进行设置。
package cvds.cldvision.com.okhttp.ui; import android.app.Application; import com.zhy.http.okhttp.OkHttpUtils; import java.util.concurrent.TimeUnit; import okhttp3.OkHttpClient; /** * Created by Administrator on 2017/1/18. */ public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); OkHttpClient okHttpClient = new OkHttpClient.Builder() // .addInterceptor(new LoggerInterceptor("TAG")) .connectTimeout(10000L, TimeUnit.MILLISECONDS) .readTimeout(10000L, TimeUnit.MILLISECONDS) //其他配置 .build(); OkHttpUtils.initClient(okHttpClient); } }
第二步:分别是get post 下载 上传
1.回调方法:
public class MyStringCallback extends StringCallback { @Override public void onError(Call call, Exception e, int i) { } @Override public void onResponse(String s, int i) { textView.setText("请求返回值"+s); } @Override public void inProgress(float progress, long total, int id) { // super.inProgress(progress, total, id); System.out.println("进度"+100 * progress); progressBar.setProgress((int)(100 * progress)); } @Override public void onAfter(int id) { // super.onAfter(id); } @Override public void onBefore(Request request, int id) { // super.onBefore(request, id); } }2获取图片:
public void getImage() { textView.setText(""); String url = "http://images.csdn.net/20150817/1.jpg"; OkHttpUtils .get()// .url(url)// .tag(this)// .build()// .connTimeOut(20000) .readTimeOut(20000) .writeTimeOut(20000) .execute(new BitmapCallback() { @Override public void onError(Call call, Exception e, int id) { textView.setText("onError:" + e.getMessage()); } @Override public void onResponse(Bitmap bitmap, int id) { Log.e("TAG", "onResponse:complete"); imageView.setImageBitmap(bitmap); } }); }3下载
public void download(){ OkHttpUtils// .get()// .url("http://vfx.mtime.cn/Video/2016/07/24/mp4/160724055620533327_480.mp4")// .build()// .execute(new FileCallBack(Environment.getExternalStorageDirectory().getAbsolutePath(),"okhttputils-2_6_2.mp4"){ @Override public void onError(Call call, Exception e, int i) { } @Override public void onResponse(File file, int i) { } @Override public void inProgress(float progress, long total, int id) { super.inProgress(progress, total, id); System.out.println("进度"+100 * progress); progressBar.setProgress((int) (100 * progress)); } }); }4post请求:
public void getDataPostByOkhttpUtils() { String url = "http://www.zhiyun-tech.com/App/Rider-M/changelog-zh.txt"; // url="http://www.391k.com/api/xapi.ashx/info.json?key=bd_hyrzjjfb4modhj&size=10&page=1"; url = "http://api.m.mtime.cn/PageSubArea/TrailerList.api"; OkHttpUtils .post() .url(url) .id(100) .build() .execute(new MyStringCallback()); }5get请求:public void getOkhttpUtils(){ String url="http://api.m.mtime.cn/PageSubArea/TrailerList.api"; OkHttpUtils.get().url(url).id(100).build().execute(new MyStringCallback()); }6使用okhttp-utils上传多个或者单个文件(带参数):public void multiFileUpload() { HashMap<String, String> map = new HashMap<>(); map.put("Action", "AppVersion"); map.put("Version", getVersionCode(getApplicationContext()) + ""); map.put("Type", "1"); // String mBaseUrl = "http://192.168.1.12:8080/FileUpload/FileUploadServlet"; // File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "okhttputils-2_6_2.mp4"); // File file2 = new File(Environment.getExternalStorageDirectory(), "test.txt"); // if (!file.exists()) // { // Toast.makeText(MainActivity.this, "文件不存在,请修改文件路径", Toast.LENGTH_SHORT).show(); // return; // } // Map<String, String> params = new HashMap<>(); // params.put("username", "哈哈"); // params.put("password", "123"); String url = "请求接口地址" ; // String url =mBaseUrl ; OkHttpUtils.post()// // .addFile("mFile", "okhttputils-2_6_2.mp4", file)// // .addFile("mFile", "server_test.txt", file2)// .url(url) .params(map)// .build()// .execute(new MyStringCallback()); }二、下面是GitHub上的原文件
okhttp-utils
对okhttp的封装类,okhttp见:https://github.com/square/okhttp.
目前对应okhttp版本
3.3.1
.用法
Android Studio
compile 'com.zhy:okhttputils:2.6.2'
Eclipse
下载最新jar:okhttputils-2_6_2.jar
注:需要同时导入okhttp和okio的jar,下载见:https://github.com/square/okhttp.
目前对以下需求进行了封装
- 一般的get请求
- 一般的post请求
- 基于Http Post的文件上传(类似表单)
- 文件下载/加载图片
- 上传下载的进度回调
- 支持取消某个请求
- 支持自定义Callback
- 支持HEAD、DELETE、PATCH、PUT
- 支持session的保持
- 支持自签名网站https的访问,提供方法设置下证书就行
配置OkhttpClient
默认情况下,将直接使用okhttp默认的配置生成OkhttpClient,如果你有任何配置,记得在Application中调用initClient
方法进行设置。
public class MyApplication extends Application
{
@Override
public void onCreate()
{
super.onCreate();
OkHttpClient okHttpClient = new OkHttpClient.Builder()
// .addInterceptor(new LoggerInterceptor("TAG"))
.connectTimeout(10000L, TimeUnit.MILLISECONDS)
.readTimeout(10000L, TimeUnit.MILLISECONDS)
//其他配置
.build();
OkHttpUtils.initClient(okHttpClient);
}
}
别忘了在AndroidManifest中设置。
对于Cookie(包含Session)
对于cookie一样,直接通过cookiejar方法配置,参考上面的配置过程。
CookieJarImpl cookieJar = new CookieJarImpl(new PersistentCookieStore(getApplicationContext()));
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.cookieJar(cookieJar)
//其他配置
.build();
OkHttpUtils.initClient(okHttpClient);
目前项目中包含:
- PersistentCookieStore //持久化cookie
- SerializableHttpCookie //持久化cookie
- MemoryCookieStore //cookie信息存在内存中
如果遇到问题,欢迎反馈,当然也可以自己实现CookieJar接口,编写cookie管理相关代码。
此外,对于持久化cookie还可以使用https://github.com/franmontiel/PersistentCookieJar.
相当于框架中只是提供了几个实现类,你可以自行定制或者选择使用。
对于Log
初始化OkhttpClient时,通过设置拦截器实现,框架中提供了一个LoggerInterceptor
,当然你可以自行实现一个Interceptor 。
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new LoggerInterceptor("TAG"))
//其他配置
.build();
OkHttpUtils.initClient(okHttpClient);
对于Https
依然是通过配置即可,框架中提供了一个类HttpsUtils
- 设置可访问所有的https网站
HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null, null, null);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
//其他配置
.build();
OkHttpUtils.initClient(okHttpClient);
- 设置具体的证书
HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(证书的inputstream, null, null);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager))
//其他配置
.build();
OkHttpUtils.initClient(okHttpClient);
- 双向认证
HttpsUtils.getSslSocketFactory(
证书的inputstream,
本地证书的inputstream,
本地证书的密码)
同样的,框架中只是提供了几个实现类,你可以自行实现SSLSocketFactory
,传入sslSocketFactory即可。
其他用法示例
GET请求
String url = "http://www.csdn.net/";
OkHttpUtils
.get()
.url(url)
.addParams("username", "hyman")
.addParams("password", "123")
.build()
.execute(new StringCallback()
{
@Override
public void onError(Request request, Exception e)
{
}
@Override
public void onResponse(String response)
{
}
});
POST请求
OkHttpUtils
.post()
.url(url)
.addParams("username", "hyman")
.addParams("password", "123")
.build()
.execute(callback);
Post JSON
OkHttpUtils
.postString()
.url(url)
.content(new Gson().toJson(new User("zhy", "123")))
.mediaType(MediaType.parse("application/json; charset=utf-8"))
.build()
.execute(new MyStringCallback());
提交一个Gson字符串到服务器端,注意:传递JSON的时候,不要通过addHeader去设置contentType,而使用.mediaType(MediaType.parse("application/json; charset=utf-8"))
.。
Post File
OkHttpUtils
.postFile()
.url(url)
.file(file)
.build()
.execute(new MyStringCallback());
将文件作为请求体,发送到服务器。
Post表单形式上传文件
OkHttpUtils.post()//
.addFile("mFile", "messenger_01.png", file)//
.addFile("mFile", "test1.txt", file2)//
.url(url)
.params(params)//
.headers(headers)//
.build()//
.execute(new MyStringCallback());
支持单个多个文件,addFile
的第一个参数为文件的key,即类别表单中<input type="file" name="mFile"/>
的name属性。
自定义CallBack
目前内部包含StringCallBack
,FileCallBack
,BitmapCallback
,可以根据自己的需求去自定义Callback,例如希望回调User对象:
public abstract class UserCallback extends Callback<User>
{
@Override
public User parseNetworkResponse(Response response) throws IOException
{
String string = response.body().string();
User user = new Gson().fromJson(string, User.class);
return user;
}
}
OkHttpUtils
.get()//
.url(url)//
.addParams("username", "hyman")//
.addParams("password", "123")//
.build()//
.execute(new UserCallback()
{
@Override
public void onError(Request request, Exception e)
{
mTv.setText("onError:" + e.getMessage());
}
@Override
public void onResponse(User response)
{
mTv.setText("onResponse:" + response.username);
}
});
通过parseNetworkResponse
回调的response进行解析,该方法运行在子线程,所以可以进行任何耗时操作,详细参见sample。
下载文件
OkHttpUtils//
.get()//
.url(url)//
.build()//
.execute(new FileCallBack(Environment.getExternalStorageDirectory().getAbsolutePath(), "gson-2.2.1.jar")//
{
@Override
public void inProgress(float progress)
{
mProgressBar.setProgress((int) (100 * progress));
}
@Override
public void onError(Request request, Exception e)
{
Log.e(TAG, "onError :" + e.getMessage());
}
@Override
public void onResponse(File file)
{
Log.e(TAG, "onResponse :" + file.getAbsolutePath());
}
});
注意下载文件可以使用FileCallback
,需要传入文件需要保存的文件夹以及文件名。
显示图片
OkHttpUtils
.get()//
.url(url)//
.build()//
.execute(new BitmapCallback()
{
@Override
public void onError(Request request, Exception e)
{
mTv.setText("onError:" + e.getMessage());
}
@Override
public void onResponse(Bitmap bitmap)
{
mImageView.setImageBitmap(bitmap);
}
});
显示图片,回调传入BitmapCallback
即可。
上传下载的进度显示
new Callback<T>()
{
//...
@Override
public void inProgress(float progress)
{
//use progress: 0 ~ 1
}
}
callback回调中有inProgress
方法,直接复写即可。
HEAD、DELETE、PUT、PATCH
OkHttpUtils
.put()//also can use delete() ,head() , patch()
.requestBody(RequestBody.create(null, "may be something"))//
.build()//
.execute(new MyStringCallback());
如果需要requestBody,例如:PUT、PATCH,自行构造进行传入。
同步的请求
Response response = OkHttpUtils
.get()//
.url(url)//
.tag(this)//
.build()//
.execute();
execute方法不传入callback即为同步的请求,返回Response。
取消单个请求
RequestCall call = OkHttpUtils.get().url(url).build();
call.cancel();
根据tag取消请求
目前对于支持的方法都添加了最后一个参数Object tag
,取消则通过OkHttpUtils.cancelTag(tag)
执行。
例如:在Activity中,当Activity销毁取消请求:
OkHttpUtils
.get()//
.url(url)//
.tag(this)//
.build()//
@Override
protected void onDestroy()
{
super.onDestroy();
//可以取消同一个tag的
OkHttpUtils.cancelTag(this);//取消以Activity.this作为tag的请求
}
比如,当前Activity页面所有的请求以Activity对象作为tag,可以在onDestory里面统一取消。
混淆
#okhttputils
-dontwarn com.zhy.http.**
-keep class com.zhy.http.**{*;}
#okhttp
-dontwarn okhttp3.**
-keep class okhttp3.**{*;}
#okio
-dontwarn okio.**
-keep class okio.**{*;}