I'm using a WevView on Android (KitKat) and I'm trying to override shouldInterceptRequest() and make my own HTTP requests - to have the requests handled by my app rather than the webview.
My point is to work around the cross-origin limitation of the webview, which arguably may not be a good idea, but that's another problem.
Something is going wrong when I'm passing the data received from my HTTP request to the WebResourceResponse object returned by the shouldInterceptRequest() method. At this point it's worth mentioning that it's my first Android app (not starting with the right task, but hey) and that my knowledge of streams on Android is limited. I feel like there is some kinf of synchronisation problem, maybe the webview expecting buffered data and my InputStream being unbuffered? Anyway, despite looking around on different forums I can't get my finger on what the problem actually is.
My code looks like this:
mWebView.setWebViewClient( new WebViewClient()
{
public WebResourceResponse shouldInterceptRequest(WebView _view, String _url)
{
URL url;
try {
url = new URL(_url);
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
}
HttpURLConnection urlConnection;
try {
urlConnection = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
e.printStackTrace();
return null;
}
WebResourceResponse response = null;
try {
response = new WebResourceResponse( urlConnection.getContentType(),
urlConnection.getContentEncoding(),
urlConnection.getInputStream() );
} catch (IOException e) {
e.printStackTrace();
} finally {
urlConnection.disconnect();
}
return response;
}
});
When I run my test app, it crashes. Logcat gives me the following logs:
W/System.err(4527): java.io.IOException: stream closed
W/System.err(4527): at com.android.okhttp.internal.http.AbstractHttpInputStream.checkNotClosed(AbstractHttpInputStream.java:68)
W/System.err(4527): at com.android.okhttp.internal.http.HttpTransport$ChunkedInputStream.available(HttpTransport.java:476)
W/System.err(4527): at dalvik.system.NativeStart.run(Native Method)
A/libc(4527): Fatal signal 6 (SIGABRT) at 0x000011af (code=-6), thread 4551 (example.webtest)
I/chromium(4527): [INFO:async_pixel_transfer_manager_android.cc(56)] Async pixel transfers not supported
I have seen a post by someone trying to do something similar (System Crash When Overriding shouldInterceptRequest in WebViewClient) but the error I encounter seems completely different.
Any ideas?
Thanks for your help!