We have some videos hosted in our platform. Let's say mydomain.com/no_auth_video_1
and mydomain.com/no_auth_video_2
, for instance. Now we added a third video mydomain.com/auth_video_3
, but this one can only be seen if you add some authentication headers to the request.
In our android app, we use a WebView
to play these videos. We have this html template:
<!-- html stuf -->
<video>
<source src="%videoUrl%"/>
</video>
<!-- html stuf -->
and once replaced the url, we load the html on the webview with:
webView.loadData(html, "text/html", "utf-8")
This works great for the no_auth
videos, but not for the video which needs the authentication. Looking for some answers, I found that we should override the shouldInterceptRequest
method on the WebViewClient:
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
String url = request.getUrl().toString();
if (urlShouldBeHandledByWebView(url)) {
return super.shouldInterceptRequest(view, url);
}
return getNewResponse(view, url);
}
private WebResourceResponse getNewResponse(WebView view, String url) {
try {
OkHttpClient okHttpClient = getMyAuthenticatedHTTPClient()
Request request = new Request.Builder()
.url(url)
.build();
final Response response = okHttpClient.newCall(request).execute();
return new WebResourceResponse(
response.header("content-type", null),
response.header("content-encoding", "utf-8"),
response.body().byteStream()
);
} catch (Exception e) {
return null;
}
}
This works intended since the query for mydomain.com/auth_video_3
is executed, and I can see that the info is retrieved properly with the debugger. Problem is that in the WebView
the video isn't loading anyway.