小编Ant*_*pov的帖子

改造离线请求和响应

我已经阅读了很多关于我的问题的问题和答案,但我仍然无法理解如何解决它。

我需要从服务器获取响应并将其存储在缓存中。之后,当设备离线时,我想使用缓存响应。当设备在线时,我想从服务器准确获取响应。

看起来没那么复杂。

这是我尝试这样做的方式(代码示例):

1)创建缓存的方法

Cache provideOkHttpCache() {
        int cacheSize = 10 * 1024 * 1024; // 10 MiB
        Cache cache = new Cache(this.getCacheDir(), cacheSize);
        return cache;
    }
Run Code Online (Sandbox Code Playgroud)

2)创建一个拦截器来修改缓存控制头

 Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() {
        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {

            CacheControl.Builder cacheBuilder = new CacheControl.Builder();
            cacheBuilder.maxAge(0, TimeUnit.SECONDS);
            cacheBuilder.maxStale(365, TimeUnit.DAYS);
            CacheControl cacheControl = cacheBuilder.build();

            Request request = chain.request();

            if (isOnline()) {
                request = request.newBuilder()
                        .cacheControl(cacheControl)
                        .build();
            }
            okhttp3.Response originalResponse = chain.proceed(request);
            if (isOnline()) {
                int maxAge = 20; …
Run Code Online (Sandbox Code Playgroud)

android caching retrofit okhttp

2
推荐指数
1
解决办法
1576
查看次数

ReplacementSpan 在 TextView 中剪切线

我需要为 TextView 中的文本使用自定义下划线。我使用 ReplacementSpan 来做到这一点。但它会在第一行的末尾剪切文本。

这是我的CustomUnderlineSpan课:

public class CustomUnderlineSpan extends ReplacementSpan {
    private int underlineColor;
    private int textColor;


    public CustomUnderlineSpan(int underlineColor, int textColor) {
        super();
        this.underlineColor = underlineColor;
        this.textColor = textColor;
    }

    @Override
    public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
        paint.setStrokeWidth(3F);

        paint.setColor(textColor);
        canvas.drawText(text, start, end, x, y, paint);

        paint.setColor(underlineColor);
        int length = (int) paint.measureText(text.subSequence(start, end).toString());
        canvas.drawLine(x, bottom, length + x, bottom, paint);
    }

    @Override …
Run Code Online (Sandbox Code Playgroud)

string android textview android-edittext spannablestring

0
推荐指数
1
解决办法
858
查看次数