shaver
shaver

Reputation: 157

logging HTTP request start and finish from embedded Android WebView

I'm looking for a way to log the requests and start/end times made by an embedded webview. I'm not able to find a way to do it so far other than rooting the phone and running tcpdump. That works for me, but I need to run this in the field, so that's not really viable. There are lots of ways to log the URL and start time, but I can't see the finish (or, bonus, the full response metadata).

shouldLoadResource could work if I could wrap the current request, but I'd have to fetch it myself with HTTP support in order to return it en masse, because there isn't enough API exposed to fully forward to the inner request. (I don't want to do that for a number of reasons, including that webview on devices doesn't use the same network stack as the HTTP classes, and because it will change the timing of subresources.)

I've been trying to find ways to turn on chromium_net debug flags to do this, but I can't figure out how do do that in the context of the WebView or system properties.

I would really rather not ship my own webcore to do this, but if needs must...

Upvotes: 7

Views: 2628

Answers (2)

bma350
bma350

Reputation: 1281

override method shouldInterceptRequest()

@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
    Log.d(LOG_TAG, "shouldInterceptRequest: " + url);

    return super.shouldInterceptRequest(view, url);
}

Upvotes: 2

Torid
Torid

Reputation: 4196

In that case, you could also add a WebViewClient (see http://developer.android.com/reference/android/webkit/WebViewClient.html). Which would look something like

WebView webView.setWebViewClient(new MyWebViewClient());

.
.
.

public class MyWebViewClient extends WebViewClient 
{
  @Override
  public boolean shouldOverrideUrlLoading(WebView view, String url) 
  {
    // Note time

    // Return false to say we want the WebView to handle the url.
    return false;
    }

  @Override 
  public void onPageFinished (WebView view, String url)
  {
    super.onPageFinished(view, url);

    // Note time
  }
}

Note that both shouldOverrideUrlLoading and onPageFinished are only called only for the main frame - they will not be called for iframes or framesets. But this should give you what you need.

Upvotes: 0

Related Questions