Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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
Android WebView加载带有自定义url和cookie的图像标记 问题:加载带有自定义url和cookie的图像标记_Android_Kotlin_Webview_Android Webview_Interceptor - Fatal编程技术网

Android WebView加载带有自定义url和cookie的图像标记 问题:加载带有自定义url和cookie的图像标记

Android WebView加载带有自定义url和cookie的图像标记 问题:加载带有自定义url和cookie的图像标记,android,kotlin,webview,android-webview,interceptor,Android,Kotlin,Webview,Android Webview,Interceptor,假设我在app/src/main/assets/build/文件夹下保存了这个html文件 尝试解决方案 在主要活动中,我正在拦截请求并使用WebViewAssetLoader class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setConte

假设我在
app/src/main/assets/build/
文件夹下保存了这个html文件


尝试解决方案 在主要活动中,我正在拦截请求并使用
WebViewAssetLoader

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setUpView()
    }

    fun setUpView() {
        val webView = findViewById<WebView>(R.id.webView)
        WebView.setWebContentsDebuggingEnabled(true)
        val assetLoader = WebViewAssetLoader.Builder()
            .addPathHandler("/assets/", WebViewAssetLoader.AssetsPathHandler(this))
            .build()
        webView.webViewClient = object : WebViewClient() {
            override fun shouldInterceptRequest(
                view: WebView?,
                request: WebResourceRequest?
            ): WebResourceResponse? {
                request?.url?.toString()?.let {
                    return if (it.startsWith("http://127.0.0.1:9000/")) {
                        if (it.contains("/api/v2")) {
                            // I would want to insert cookie in headers
                            // and change origin from 127.0.0.1:9000 to let's say app.server.com
                            // HOW DO I achieve that here?
                            super.shouldInterceptRequest(view, 
                                CustomWebResourceRequest(request))
                        } else {
                            val newUrl = it.replace(
                                "http://127.0.0.1:9000/",
                                "https://appassets.androidplatform.net/assets/build/"
                            )
                            assetLoader.shouldInterceptRequest(Uri.parse(newUrl))
                        }
                    } else {
                        super.shouldInterceptRequest(view, request)
                    }
                } ?: run {
                    return super.shouldInterceptRequest(view, request)
                }
            }
        }
        webView.loadUrl("http://127.0.0.1:9000/index.html")
    }
}
上面的代码完美地加载了html。 但它不会加载图像。我做错了什么

class CustomWebResourceRequest(
    private val request: WebResourceRequest
): WebResourceRequest {
    override fun getUrl(): Uri {
        val newUrl = request.url.toString().replace(
            "http://127.0.0.1:9000/",
            "https://app.server.com/"
        )
        return Uri.parse(newUrl)
    }

    override fun isForMainFrame(): Boolean {
        return request.isForMainFrame
    }

    override fun isRedirect(): Boolean {
        return request.isRedirect
    }

    override fun hasGesture(): Boolean {
        return request.hasGesture()
    }

    override fun getMethod(): String {
        return request.method
    }

    override fun getRequestHeaders(): MutableMap<String, String> {
        val headers = request.requestHeaders
        headers["Cookie"] = "some-cookie-value"
        return headers
    }
}