Blocking shouldOverrideUrlLoading in android webview - android

I am trying to develop a custom webview app where in only google safe browsing sites will be opened in webview.
For that I need to send url requested in webview to google first, wait for google safe browsing api response and if safe then load the url in webview.
For that I am intercepting url in shouldOverrideUrlLoading, and sending an http request from there. But since shouldOverrideUrlLoading is called on UI thread, I get networkonmainthread exception. I can send request in async task but it wont stop webview from loading the url.
Is there any way by which I can block shouldOverrideUrlLoading without getting ANR or networkonmainthread exception?

Related

How to trigger css and js load with Android WebView loadData

How can I make a WebView request the CSS and javascript links when I load HTML externally into the WebView?
This question is similar to:
https://stackoverflow.com/questions/34608714/css-and-js-files-not-loading-in-android-webview
but we are loading everything from a server. In order to support SSL Pinning we override the
loadUrl(String url)
method (and will override the others) and handle the request with an HttpClient.execute call. When we receive the data back from the network, then we push it into the webview with
WebView.loadData(event.getContent(), event.getMimeType(), event.getEncoding());
How should we implement this design to retrieve data externally and then load it into the webview? The approach above shows the HTML fine but the CSS files are not loaded.
You don't. Use the loadDataWithBaseURL. Then the WebView will fire requests to load the css/js/png. The tricky part is you can override WebView.loadUrl for the initial request but then override WebViewClient.shouldInterceptRequest for each of the css/js/png requests. In there you can fire your network request and return the response as a stream (WebResourceResponse).

Getting callback from payment gateway in webview

I am using Atom Payment Gateway for payments in my Android app. But this provider doesn't have an SDK for mobile platforms, also I cannot choose another provider because my client has been using Atom PG for their website for a long time.
So to make it work, I am now trying to call it in a webview in my app. All goes well until the last step except that I am not able to get the response from the PG upon completion of transaction.
As per their documentation:
After the completion of the transaction, the response will be posted back to the url provided by the merchant.
I already tried setting the return url to my reverse domain name and then setting an intent-filter but that doesn't seem to work.
Is there any method by which I can get the response that the PG "posts back" to the return url?
Here is an idea,
On getting the post request on your return url, you can parse that response and then via javascript send that to the JavascriptInterface linked to your WebView in the app
Intercepting POST data has been discussed:
How to intercept POST data in an android webview
Intercept POST requests in a WebView
Try this project : https://github.com/KeejOow/android-post-webview

Android-the method loadUrl() in webView sends POST or GET request?

i'm working with webview recently and i found 2 methods to load an URL in webView.
For postUrl() the doc said clearly it sends a POST request, but it didn't say anything about loadUrl().
can anybody tell me anything about it?

Android get request and response objects from webview

is there a way to get request and response objects from a webview?
for requests made from some webpage running in my webview, i want to intercept the full http request object(the headers, http method used, http body etc) and divert and send across that request into another channel.
For responses received from the webview, i want to do the same and get the object and its properties.
So far i have looked at the webviewClient android class which allows you to intercept url links executed by a webpage and intercept the resources it loads.
However, what i want to intercept, is any actual http requests the webpage makes. is this possible in Android webview?
thanks
That is not directly possible. You are welcome to write an HTTP proxy, then attempt to get WebView to work with that (e.g., see if it supports the http.proxyHost and http.proxyPort system properties).

Error redirection in WebView

I'm developing a browser-like application in android using WebView.
While loading the URL, I want to display a specific page/message/toast whenever server returns 404 response. I tried using shouldOverrideUrlLoading, but it can filter URLs based on URL string only. Is there any good way to get the server's response code within WebViewClient or WebChromeClient (without loading the URL twice)?
override the method onReceivedError of WebViewclient

Categories

Resources