2

I'm using the loadUrl() method in an activity's onCreate() method to load content from an external web server into a webview (I'm building an adroid app).

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  super.loadUrl("http://server-name/path/to/my/server/app/");
}

On the server I check the ACCEPT HTTP header to decide which data format I will deliver to a client.

Now the problem is: the loadUrl() method seems to always send the ACCEPT header value "application/xml, ...", which will cause my server to deliver data not as nicely layouted html (this is what I want for the webview), but rather as pure xml (which is good for e.g. some web service client).

So what I want to do is to set the ACCEPT HTTP header to "text/html", but the loadUrl() method won't allow me to do that.

I checked out the source code for cordova/android and found, that the loadUrl() method (in CordovaWebView.java) finally calls the (android sdk) WebView's loadUrl() method, which is overloaded to take some additional http headers. But I can't access that loadUrl method through normal cordova use.

So I edited the CordovaWebView's loadUrlNow() method, which calls the WebView's loadUrl() method and hardcoded an extra header:

void loadUrlNow(String url) {
  ...
  Map<String, String> extraHeaders = new HashMap<String, String>();
  extraHeaders.put("ACCEPT", "text/html");
  super.loadUrl(url, extraHeaders);
}

then compiled it, replaced the original cordova.jar with my own and rebuild my android app. But if I now start the app and check the ACCEPT HTTP header on server side, the ACCEPT header didn't change. It's value is the very same than before hardcoding the extra header; it seems that I can't change that header.

So, is there any way to set the ACCEPT http header when calling the loadUrl() method?

rsc
  • 115
  • 2
  • 6

1 Answers1

1

Okay, I found the answer by myself. I found out, that I can't override headers sent by the WebView; if I do, they get overridden by the WebView's default values as the ADT documentation says here (see method loadUrl, parameter additionalHttpHeaders):

http://developer.android.com/reference/android/webkit/WebView.html#loadUrl(java.lang.String, java.util.Map)

Note that if this map contains any of the headers that are set by default by this WebView, such as those controlling caching, accept types or the User-Agent, their values may be overriden by this WebView's defaults.

I'm not sure, if the documentation was that clear two weeks ago (timestamp is 13 Sep 2012). As a workaround I'm trying setting a custom HTTP header like *MYAPP_ACCEPT* to "text/html" which I can evaluate on server side. To add this additional HTTP header I need to use my self-compiled cordova lib, though.

rsc
  • 115
  • 2
  • 6