英文:
How do you add extra headers into request in Android GeckoView?
问题
我在Mozilla的错误跟踪网站上找到了这个主题。看起来这个问题已经解决。然而,当我尝试在Android Studio中从Maven导入库的最新版本时,特别是版本org.mozilla.geckoview:geckoview-nightly:100.0.20220308100756
。
我只能使用GeckoSession.loadUri(uri)
这个方法签名。期望的GeckoSession.loadUri(uri, extraHeaders)
似乎找不到。有人对此有什么见解吗?我担心这里的文档相当稀缺。
英文:
I found this thread on the Mozilla bug tracking website. It seems like the issue was addressed. However, when I go to import a recent version of the library from maven in Android Studio. Specifically version org.mozilla.geckoview:geckoview-nightly:100.0.20220308100756
.
The only method signature I am able to use is GeckoSession.loadUri(uri)
. The desired GeckoSession.loadUri(uri, extraHeaders)
is nowhere to be found. Anyone have any insights into this? I am afraid that the documentation is quite sparse around here.
答案1
得分: 1
为了实现这一点,我使用了GeckoSessions的Loader,它允许您使用类似Builder的格式传递值。
为此,我会这样写:
Map <String, String> extraHeaders = Map.of("Key 1", "Value 1", "Key 2", "Value 2", 等等);
GeckoSession.Loader loader = new GeckoSession.Loader();
loader.additionalHeaders(extraHeaders);
loader.uri("https://example.com");
geckoSession.load(loader);
值得注意的是,理论上您不应该需要将每个方法分配给加载程序,而是应该使用更传统的Builder-like格式。但根据我的经验,如果您在传递之前没有在Map中声明https标头,GeckoSession似乎无法成功加载它们,所以这是最简单的方法。
英文:
To achieve this I make use of GeckoSessions' Loader, which allows you to use a Builder-like format to pass in values.
To do this, I'd write:
Map <String, String> extraHeaders = Map.of("Key 1", "Value 1", "Key 2", "Value 2", etc.);
GeckoSession.Loader loader = new GeckoSession.Loader();
loader.additionalHeaders(extraHeaders);
loader.uri("https://example.com");
geckoSession.load(loader);
Worth noting that theoretically you shouldn't need to assign each method to the loader like this and instead use a more conventional Builder-like format, but in my experience GeckoSession seems to fail at loading in https headers when you don't have them already declared in a Map before passing, so this is easiest.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论