Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Android中使用OkHttp缓存Jsoup请求_Java_Android_Kotlin_Jsoup_Okhttp - Fatal编程技术网

Java 如何在Android中使用OkHttp缓存Jsoup请求

Java 如何在Android中使用OkHttp缓存Jsoup请求,java,android,kotlin,jsoup,okhttp,Java,Android,Kotlin,Jsoup,Okhttp,基本上,我想要实现的是使用OkHttp作为请求的http客户机,缓存我使用Jsoup发出的请求 这是我已经尝试过的 对于Okhttp客户端 var client = OkHttpClient() val cacheSize = (APP_CACHE_SIZE).toLong() // 20MB val myCache = Cache(appContext.cacheDir, cacheSize) client.newBuilder()

基本上,我想要实现的是使用OkHttp作为请求的http客户机,缓存我使用Jsoup发出的请求

这是我已经尝试过的 对于Okhttp客户端

var client = OkHttpClient()
val cacheSize = (APP_CACHE_SIZE).toLong()   // 20MB
        val myCache = Cache(appContext.cacheDir, cacheSize)
         client.newBuilder()
            .cache(myCache)
            .addInterceptor { chain ->   // Interceptor is for adding cache to the app
                var request = chain.request()
                val cacheControl = CacheControl.Builder()
                    .maxStale(APP_CACHE_MAX_LIFETIME, TimeUnit.SECONDS)
                    .build()
                request = request.newBuilder()
                    .cacheControl(cacheControl)
                    .build()
                chain.proceed(request)
            }
            .connectTimeout(10000, TimeUnit.MILLISECONDS)
            .readTimeout(10000, TimeUnit.MILLISECONDS)
            .writeTimeout(10000, TimeUnit.MILLISECONDS)
然后,我使用这个客户机发出一个新请求,将其与Jsoup一起使用

val request = Request.Builder().url("mylink.com").get()
val req = App.client.newCall(request.build())
val ongoingBody  = Jsoup.parse(req.execute().body!!.string())
我正常得到结果,但缓存没有发生

我做错了吗