duzechao / okhttputils Goto Github PK
View Code? Open in Web Editor NEW对OkHttp进行封装,网络请求失败自动查询本地缓存,上传文件等功能
对OkHttp进行封装,网络请求失败自动查询本地缓存,上传文件等功能
getData(CacheType.CACHED_ELSE_NETWORK);//报错
getData(CacheType.NETWORK_ELSE_CACHED);//可以正常使用的
okHttpUtils = new OKHttpUtils.Builder(this).interceptors(new AgainInterceptor()).build();//这样初始化还不对的,请多多指教
class LoggingInterceptor implements Interceptor {
@OverRide public Response intercept(Interceptor.Chain chain) throws IOException {
Request request = chain.request();
long t1 = System.nanoTime();
System.out.println(String.format("Sending request %s on %s%n%s",
request.url(), chain.connection(), request.headers()));
Response response = chain.proceed(request);
long t2 = System.nanoTime();
System.out.println(String.format("Received response for %s in %.1fms%n%s",
response.request().url(), (t2 - t1) / 1e6d, response.headers()));
return response;
}
}
class AgainInterceptor implements Interceptor {
@OverRide public Response intercept(Interceptor.Chain chain) throws IOException {
OkHttpClient client = new OkHttpClient();
client.interceptors().add(new LoggingInterceptor());
Request request = new Request.Builder()
.url("http://www.publicobject.com/helloworld.txt")
.header("User-Agent", "OkHttp Example")
.build();
Response response = client.newCall(request).execute();
response.body().close();
return response;
}
}
NETWORK_ELSE_CACHED 无本地缓存 第一次断网进入 闪退
private Callback Callback1 = new Callback() {
@OverRide
public void onFailure(Call call, IOException e) {
onFail(e);
}
@Override
public void onResponse(Call call,final Response response) throws IOException {
if(response!=null){
mtvOkHttp.post(new Runnable() {
@Override
public void run() {
System.out.println(response.body().toString());
mtvOkHttp.setText(response.body().toString());
}
});
}
}
};
返回的结果为:System.out: okhttp3.internal.http.RealResponseBody@41746480
为什么返回不了实际文本内容呢?请楼主多多指教
在OkHttp 2.6 下,
public Builder cacheControl(CacheControl cacheControl) {
String value = cacheControl.toString();
if (value.isEmpty()) return removeHeader("Cache-Control");
return header("Cache-Control", value);
}
设置了策略会覆盖头字段的,所以头字段写缓存字段 没必要。
然后在OkHttp 3.0下
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta3'
compile 'com.squareup.okhttp3:okhttp:3.0.0-RC1'
用最新的引用,cacheControl无效,我研究了一下午,题主可以一起研究下
去掉了在拦截器添加缓存时间的设置 ,那缓存是需要服务器来返回响应头吗?响应头的缓存参数由服务器设置。请楼主解答。如果服务器不支持缓存,又不加自定义拦截器设置响应头缓存参数,楼主是靠什么来设置缓存的?具体类代码见:https://github.com/duzechao/OKHttpUtils/blob/master/okhttputilslib/src/main/java/git/dzc/okhttputilslib/OKHttpUtils.java
/git.dzc.okhttputils I/aaaaaa: onResponse upload server is on
[ 04-01 10:43:16.294 2908: 2908 E/ ]
process stopped due to unexpected signal 13
上传图片,报这个错:signal 13的意思 #define SIGSEGV 无效内存引用
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.