Created
February 23, 2017 22:08
-
-
Save kmerrell42/b4ff31733c562a3262ee9a42f5704a89 to your computer and use it in GitHub Desktop.
Using headers from a WebView.load(...) response. Kinda ugly implementation since it requires exposing the networking implementation at the "view" level.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.mercury.monkeybutt.webviewintercept; | |
import android.annotation.TargetApi; | |
import android.os.Build; | |
import android.support.annotation.NonNull; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.webkit.WebResourceRequest; | |
import android.webkit.WebResourceResponse; | |
import android.webkit.WebView; | |
import android.webkit.WebViewClient; | |
import okhttp3.Call; | |
import okhttp3.OkHttpClient; | |
import okhttp3.Request; | |
import okhttp3.Response; | |
public class WebViewActivity extends AppCompatActivity { | |
private OkHttpClient okHttpClient = new OkHttpClient.Builder().build(); // TODO: Make this a singleton for the app | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_web_view); | |
WebView webView = (WebView) findViewById(R.id.webView); | |
webView.setWebViewClient(new WebViewClient() { | |
@SuppressWarnings("deprecation") // From API 21 we should use another overload | |
@Override | |
public WebResourceResponse shouldInterceptRequest(WebView view, String url) { | |
return handleRequest(url); | |
} | |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
@Override | |
public WebResourceResponse shouldInterceptRequest(@NonNull WebView view, @NonNull WebResourceRequest request) { | |
return handleRequest(request.getUrl().toString()); | |
} | |
private WebResourceResponse handleRequest(String url) { | |
try { | |
final Call call = okHttpClient.newCall(new Request.Builder() | |
.url(url) | |
// TODO: Add custom headers | |
.build() | |
); | |
final Response response = call.execute(); | |
// TODO: Probably need to go back to the UI thread, verify first | |
// TODO: Get the title from some custom "x-" header instead of using the response code | |
updateScreenTitle(Integer.toString(response.code())); | |
return new WebResourceResponse( | |
response.header("content-type", "text/plain"), // You can set something other as default content-type | |
response.header("content-encoding", "utf-8"), // Again, you can set another encoding as default | |
response.body().byteStream() | |
); | |
} catch (Exception e) { | |
// TODO: Figure out how to show a custom error screen when we fail | |
return null; | |
} | |
} | |
}); | |
webView.loadUrl("http://www.google.com"); | |
} | |
private void updateScreenTitle(String title) { | |
setTitle(title); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment