सामग्री पर जाएँ
Görkem KaraLead Mobile Developer

Android WebView shouldInterceptRequest: एडवांस्ड फ़ीचर्स

4 मिनट में पढ़ें

Android WebView control using shouldInterceptRequest for blocking ads and optimizing data.

shouldInterceptRequest, WebViewClient का एक method है जिसे Android आपके WebView की हर resource request (जैसे images, scripts और styles) के लिए कॉल करता है। इसे अपने client में override करें और request ब्लॉक करने, बदलने या local डेटा से जवाब देने के लिए WebResourceResponse return करें; null return करेंगे तो WebView resource को सामान्य तरीके से लोड करेगा। नीचे requests को redirect और modify करने, ऐड ब्लॉकिंग, डेटा compression, local caching और authentication headers जोड़ने के काम करते उदाहरण हैं।

shouldInterceptRequest क्या है, और यह आपके लिए क्यों ज़रूरी है?

आसान शब्दों में, shouldInterceptRequest आपके WebView की हर network request के लिए एक दरबान की तरह है। जब WebView के अंदर कोई पेज कोई resource (जैसे images, scripts या styles) लाने की कोशिश करता है, तो shouldInterceptRequest आपको वह request बीच में पकड़ने, जाँचने और यह तय करने देता है कि उसे जाने दें, बदलें या पूरी तरह ब्लॉक कर दें।

एक CustomWebViewClient में बेसिक shouldInterceptRequest ऐसे सेट करें:

class CustomWebViewClient : WebViewClient() {
    override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest?): WebResourceResponse? {
        // Your custom logic goes here
        return super.shouldInterceptRequest(view, request)
    }
}

Requests को redirect और modify करना

मान लीजिए आप किसी resource का cached version लोड करना चाहते हैं या bandwidth बचाने के लिए किसी URL को redirect करना चाहते हैं। shouldInterceptRequest से आप माँगे गए resource को आसानी से किसी दूसरे से बदल सकते हैं।

override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest?): WebResourceResponse? {
    val url = request?.url ?: return null
    // Serve the logo from the app's assets instead of downloading it again
    if (url.host == "example.com" && url.path == "/images/logo.png") {
        val context = view?.context ?: return null
        return WebResourceResponse(
            "image/png", null,
            context.assets.open("web/logo.png")
        )
    }
    return super.shouldInterceptRequest(view, request)
}

टिप: Redirects में हद से ज़्यादा न जाएँ। कई requests redirect करने से WebView की performance धीमी हो सकती है, ख़ासकर low-end डिवाइसेज़ पर।

WebView में ऐड ब्लॉकिंग

सच कहें तो ऐड हर जगह हैं, और कभी-कभी यूज़र अनुभव पर हावी हो जाते हैं। shouldInterceptRequest से आप इन परेशान करने वाले ऐड को उनके URLs के आधार पर ब्लॉक कर सकते हैं।

val blockedUrls = listOf("adserver.com", "trackersite.com")
override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest?): WebResourceResponse? {
    val url = request?.url.toString()
    if (blockedUrls.any { url.contains(it) }) {
        return WebResourceResponse("text/plain", "utf-8", ByteArrayInputStream("".toByteArray()))
    }
    return super.shouldInterceptRequest(view, request)
}

इस उदाहरण में हम ब्लॉक किए गए URLs की एक सिंपल लिस्ट इस्तेमाल करते हैं। अगर माँगा गया URL लिस्ट के किसी URL से मेल खाता है, तो हम खाली response return करते हैं, जिससे ऐड ब्लॉक हो जाता है।

डेटा compression: bandwidth बचाएँ

अगर आपका ऐप भारी डेटा वाले बहुत सारे resources लोड करता है, तो भेजने से पहले डेटा compress करके bandwidth कम कर सकते हैं। gzip compression ऐसे सेट करें:

override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest?): WebResourceResponse? {
    val connection = URL(request?.url.toString()).openConnection()
    connection.setRequestProperty("Accept-Encoding", "gzip")

    val inputStream = if (connection.getHeaderField("Content-Encoding") == "gzip") {
        GZIPInputStream(connection.getInputStream())
    } else {
        connection.getInputStream()
    }

    return WebResourceResponse(
        "text/html", "utf-8", inputStream
    )
}

Compress क्यों करें?

Compression से bandwidth बचती है और loading time घटता है, जो सीमित डेटा प्लान वाले या धीमे कनेक्शन वाले इलाक़ों के यूज़र्स के लिए बहुत ज़रूरी है।

Resources को local cache करना

मान लीजिए आप कुछ resources सिर्फ़ एक बार लोड करके बाद के लिए cache करना चाहते हैं। इससे performance काफ़ी बेहतर हो सकती है, ख़ासकर अगर आपका ऐप एक ही resources बार-बार इस्तेमाल करता है।

override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest?): WebResourceResponse? {
    val url = request?.url.toString()
    val cachedResponse = getFromCache(url)

    return if (cachedResponse != null) {
        WebResourceResponse("text/html", "utf-8", ByteArrayInputStream(cachedResponse))
    } else {
        super.shouldInterceptRequest(view, request)
    }
}

private fun getFromCache(url: String): ByteArray? {
    // Implement caching logic here
    return null
}

इस उदाहरण में getFromCache माँगे गए resource को local storage से लाने की कोशिश करता है। मिल जाए तो cached response return करता है; नहीं तो request को सामान्य तरीके से आगे जाने देता है।

Authentication headers जोड़ना

जिन resources के लिए authentication चाहिए, जैसे APIs, उनके लिए आप अपनी requests में custom headers जोड़ सकते हैं। ऐसे:

override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest?): WebResourceResponse? {
    val connection = URL(request?.url.toString()).openConnection() as HttpURLConnection
    connection.setRequestProperty("Authorization", "Bearer YOUR_TOKEN")

    val inputStream = connection.inputStream
    return WebResourceResponse(
        "text/html", "utf-8", inputStream
    )
}

इस उदाहरण में हम हर request में एक Authorization header जोड़ रहे हैं। सुरक्षित resources एक्सेस करते समय यह ख़ास तौर पर काम आता है।


Android के WebView के साथ shouldInterceptRequest इस्तेमाल करने से ऐप के अंदर वेब ब्राउज़िंग को कंट्रोल, optimise और सुरक्षित करने की ढेरों संभावनाएँ खुलती हैं। चाहे आप ऐड ब्लॉक कर रहे हों, डेटा compress कर रहे हों, resources cache कर रहे हों या authentication जोड़ रहे हों, ये techniques performance और यूज़र अनुभव दोनों को काफ़ी बेहतर कर सकती हैं। इन टिप्स के साथ प्रयोग करें, और WebView को customise करते समय यूज़र अनुभव का ध्यान रखना न भूलें। अगर आपको उसी WebView से session cookies भी चाहिए, तो Kotlin में Android WebView से cookies कैसे निकालें देखें। और अगर आप ऐसे ऐप पर काम कर रहे हैं जिसमें WebView के अंदर request interception, caching या authenticated कंटेंट चाहिए, तो मोबाइल ऐप डेवलपमेंट या कंसल्टिंग के लिए संपर्क पेज से मुझसे बात कर सकते हैं।

आख़िरी अपडेट: लेखक: Görkem KARA

#android webview#webview

What did you think of this post?

टिप्पणियाँ

अभी कोई टिप्पणी नहीं। पहली टिप्पणी आप लिखें।

टिप्पणी लिखें

टिप्पणियाँ मंज़ूरी के बाद दिखती हैं।

आपकी टिप्पणी बिना नाम के पब्लिश होती है; ईमेल सिर्फ़ इसी टिप्पणी के लिए है और पब्लिश नहीं होता। प्राइवेसी नोटिस

सुरक्षा जाँच भेजते समय अपने-आप होती है