科尔多瓦:如何设置HTTP ACCEPT标头调用Android应用程序使用loadURL()的时候? [英] Cordova: How to set HTTP ACCEPT header when calling loadUrl() in Android App?

查看:341
本文介绍了科尔多瓦:如何设置HTTP ACCEPT标头调用Android应用程序使用loadURL()的时候?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个活动的onCreate()方法中使用loadURL()方法从外部Web服务器内容加载到网页视图(我建立一个应用程序的Adroid)。

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/");
}

在我入住的是接受HTTP头,以决定哪些数据格式,我将提供给客户端的服务器。

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

现在的问题是:使用loadURL()方法似乎总发送Accept头值应用程序/ XML,...,这将导致我的服务器来提供数据不是很好layouted HTML(这就是我想要的对于web视图),而是作为纯XML(这有利于例如一些Web服务客户端)。

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).

所以,我想要做的是设置接受HTTP标头text / html的,但使用loadURL()方法不会允许我这样做。

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.

我检查了源$ C ​​$ C为科尔多瓦/ Android和发现,使用loadURL()方法(在CordovaWebView.java)最后调用(Android SDK中)的WebView使用loadURL()方法,该方法被重载采取一些额外的HTTP标头。但我无法通过正常科尔多瓦利用访问使用loadURL方法。

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.

所以我编辑的CordovaWebView的loadUrlNow()方法中,它调用的WebView使用loadURL()方法和硬codeD额外的标题:

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);
}

再编译它,我自己取代了原来的cordova.jar,重建我的Andr​​oid应用程序。但是,如果我现在启动应用程序,并检查接受服务器端的HTTP标头,接收报头并没有改变。它的值比硬编码额外的头之前非常相似;看来我不能改变这个头。

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.

那么,有没有什么办法调用使用loadURL()方法时,设置接受HTTP头?

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

推荐答案

好吧,我发现我自己的答案。我发现,我不能覆盖由发送的WebView头;如果我这样做,他们得到了的WebView默认值作为ADT文件说,这里覆盖(请参阅法的使用loadURL 的,参数的 additionalHttpHeaders 的):

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中)

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

请注意,如果此映射包含任何默认情况下该WebView中设置的标头,如控制缓存,接受类型或用户代理,其值可以通过这个的WebView默认覆盖。

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.

我不知道,如果文件是明确的两个星期前(时间戳为2012年9月13日)。作为一种变通方法我试图设置自定义HTTP标头像* MYAPP_ACCEPT ​​为text / html的,我可以在服务器端进行评估。要添加此额外的HTTP标头,我需要用我的自编科尔多瓦lib下,虽然。

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.

这篇关于科尔多瓦:如何设置HTTP ACCEPT标头调用Android应用程序使用loadURL()的时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆