如何在 webview 中加载桌面视图而不是移动视图 [英] How to load Desktop view instead of mobile view in webview

查看:39
本文介绍了如何在 webview 中加载桌面视图而不是移动视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个 android 网络应用程序,当我在模拟器上运行时,桌面版本会显示,但当我在我的 android 设备上运行时,移动视图会加载.我想要的是在我的设备上加载桌面视图

Am designing an android web app, when i run on the emulator the desktop version shows but when i run on my android device mobile view rather loads. What i want is to load the desktop view on my device

推荐答案

服务器使用 HTTP 请求中的 User-Agent 标头来确定设备是移动设备还是桌面设备.您可以使用修改后的 User-Agent 标头请求网站,以便服务器认为它是桌面设备.

The server uses the User-Agent header in the HTTP request to determine if the device is mobile or desktop. You can request the website with modified User-Agent header so that the server will think it's desktop device.

安卓代码:

Map<String, String> headers = new Map<String, String>();
headers.put("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0");
loadUrl("www.google.com",  headers);

欲了解更多信息:http://developer.android.com/reference/android/webkit/WebView.html#loadUrl(java.lang.String, java.util.Map)

http://en.wikipedia.org/wiki/Mobile_device_detection

对于可用的 User-Agent 字符串列表:http://www.useragentstring.com/Firefox25.0_id_19710.php

And for a list of available User-Agent strings: http://www.useragentstring.com/Firefox25.0_id_19710.php

再次查看WebView文档后,我在loadUrl函数的描述中发现了这一点:

After looking again at the WebView documentation, I found this in the description of the loadUrl function:

请注意,如果此映射包含由默认通过这个 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.

这意味着,你不能像我上面写的那样覆盖 User-Agent.在互联网上搜索了一下后,我发现了这个: Android webview中的setUserAgentString对loadURL()中使用的HTTP头没有影响

Which means, you cant override User-Agent in the way I wrote above. After searching a bit on the internet I found this: setUserAgentString in Android webview has no effect on HTTP header used in loadURL()

并想出了这个代码:

webview.getSettings().setUserAgentString("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0");
webview.loadUrl("www.google.co.il");

请尝试一下,看看是否能解决您的问题.

Please try it and check if that solves your problem.

这篇关于如何在 webview 中加载桌面视图而不是移动视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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