我在一个示例应用程序的工作才能够简单的HTML数据加载到web视图不扭曲的内容。在我的活动,我使用的org.apache.http包连接到一个网站,并检索它在一个私有方法openGoogleHomePage内容()。
I am working on an example app to be able to load simple html data into a webview without distorting the content. In my Activity, I am using the org.apache.http packages to connect to a website and retrieve it's content in a private method openGoogleHomePage().
DefaultHttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://www.google.com");
get.setHeader("Content-Type", "application/x-www-form-urlencoded");
get.setHeader("User-Agent","Mozilla/5.0 (Linux; U; Android 2.1-update1; de-de; HTC Desire 1.19.161.5 Build/ERE27) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17");
HttpResponse response = client.execute(get);
String data = new BasicResponseHandler().handleResponse(response);
data = data.replaceAll("%", "%");
return data;
当我数据比较在打开实际的HTML内容的响应 http://www.google.com 在firefox浏览器,内容是一样的。然而,当我尝试加载该数据到网页视图,我没有看到这样的事情。
When I compare 'data' to the actual html content response upon opening http://www.google.com on firefox browser, the content is the same. However, when I try to load this 'data' into a webview, I don't see anything like it.
LinearLayout layout = (LinearLayout) View.inflate(this, R.layout.main, null);
setContentView(layout);
WebView browser = (WebView) layout.findViewById(R.id.webPage);
browserSettings = browser.getSettings();
browserSettings.setJavaScriptCanOpenWindowsAutomatically(true);
browserSettings.setJavaScriptEnabled(true);
browserSettings.setSavePassword(false);
browserSettings.setSaveFormData(false);
browserSettings.setSupportZoom(false);
browser.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
String htmlData = openGoogleHomePage();
if (htmlData != null) {
browser.loadData(htmlData, "text/html", HTTP.UTF_8);
}
Whatelse我有我的code更改为能够查看完全按照我在PC上的浏览器做的WebView?在另一方面,不通过使用HTTP包调用的URL,当然使用webview.loadUrl(http://www.gooogle.com)从web视图而不是调用URL的艰苦的过程去,检索移动版搜索巨头的页面,这是比PC浏览器版本的方式不同。
Whatelse do I have to change in my code to be able to view in the webview exactly as I do in the browser on a PC? On the other hand, without going through the painstaking process of invoking the URL using the http packages, and instead invoking the url from the webview using webview.loadUrl("http://www.gooogle.com") of course, retrieves the mobile version of the search-giant's page, which is way different than the PC browser version.
总之,我想在我的WebView看正是我看到在PC浏览器,在我的家乡习俗code处理HTTP调用后。
In short, I want to see in my webview exactly what I see on a PC browser, after handling the http invocations in my native custom code.
另外,如果有人能证明在处理URL重定向一些三分球,这将是有很大帮助。
Also, if someone could show some pointers at handling url redirects, that would be of much help.
推荐答案
一些移动设备检测可以基于用户代理。尝试将用户代理在WebView中加入多一个设置,您的WebView使用台式机产品。
Some mobile device detection could be based on the user-agent. Try setting the UserAgent in the WebView to use Desktop counterpart by adding one more setting to your WebView.
browserSettings.setUserAgentString("Mozilla/5.0");
相关推荐
最新文章