5

I am overriding webViewClient.shouldInterceptRequest() to return common resources like images and js files from asset folder.But this is available for android >=3 .is there any alternative to use for android 2.x

I found onLoadResource() to provide similar behaviour but its return type is void

vishesh
  • 2,007
  • 6
  • 32
  • 67

1 Answers1

4

I've been digging this for a while, and the practical way I found so far to achieve the similar purpose is to implement a local HTTP proxy for this web view.

To set a proxy for webview, see this question: WebView android proxy

There're many open source HTTP proxy written in Java, like LittleProxy.


Yet another possible but complex way: use shouldOverrideUrlLoading() to capture all page requests but always return true. Fetch the content of page with your own code, then parse and replace all remote resources with local cached copies, and finally load the modified content with WebView.loadData().

Community
  • 1
  • 1
Oasis Feng
  • 7,490
  • 3
  • 31
  • 44
  • for me, shouldOverrideUrlLoading was not called on Jellybean when loading assets - it was only called when i loaded urls. i'm switching to use a proxy. – chaqke Jul 08 '13 at 19:43
  • @chaqke As the method name suggests, it only applies to URL loading. Still you can load your assets in the manner of URL, and feed that URL loading with your local assets. – Oasis Feng Jul 09 '13 at 16:18
  • the API-level-11 shouldInterceptRequest was called for assets when loading a local html file, but the API-level-1 shouldOverrideUrlLoading was not called. i required processing (decrypting) assets at loading time, without a guarantee of exactly which assets would be linked by the local html. for anyone in a similar situation, the proxy seems like a better idea then programmatically finding/loading/replacing assets that had already been improperly loaded by the webview. – chaqke Jul 24 '13 at 23:22