简体   繁体   English

启动平板电脑时,Webview URL重定向/更改

[英]Webview URL redirects/changes when launch tablet

I'm using webview in my application and giving hardcoded URL using the code. 我在应用程序中使用Webview,并使用代码提供了硬编码的URL。 All things are working perfectly fine, but one thing came across when I was testing it on 10.1" tablet. The URL which I have provided works fine with the phone but on tablet it redirects itself to the desktop version of the URL. 一切正常,但在我在10.1英寸平板电脑上进行测试时碰到了一件事。我提供的URL在手机上可以正常工作,但在平板电脑上,它将自身重定向到该URL的桌面版本。

Any idea how to prevent that from happening. 任何想法如何防止这种情况发生。 I'm using this URL: http://search.yahoo.com/mobile/s?submit=oneSearch&.intl=us&.lang=en&.tsrc=yahoo&.sep=fp&x=0&y=0&p=bike 我正在使用以下URL: http : //search.yahoo.com/mobile/s?submit= oneSearch& .intl= us& .lang= en& .tsrc= yahoo& .sep= fp& x=0&y=0&p=bike

I have specifically mentioned mobile in the URL, but still it is redirecting. 我在URL中特别提到了移动设备,但它仍在重定向。 Any idea..?? 任何想法..??

In order to handle the redirecting you need to use WebViewClient 为了处理重定向,您需要使用WebViewClient

Then override the shouldOverrideUrlLoading method. 然后重写shouldOverrideUrlLoading方法。

"Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView. If WebViewClient is not provided, by default WebView will ask Activity Manager to choose the proper handler for the url. If WebViewClient is provided, return true means the host application handles the url, while return false means the current WebView handles the url." “当即将在当前WebView中加载新的URL时,给主机应用程序一个接管控制的机会。如果未提供WebViewClient,则默认情况下,WebView将要求Activity Manager为该URL选择适当的处理程序。如果WebViewClient被提供,返回true表示主机应用程序处理该URL,而返回false表示当前WebView处理该URL。”

need to use webview client 需要使用webview客户端

        WebView   web = (WebView) findViewById(R.id.webView1);  
        web.loadUrl(myurl.trim());
    web.setWebViewClient(new HelloWebViewClient());

private class HelloWebViewClient extends WebViewClient  
{

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        super.onPageStarted(view, url, favicon);        
        Log.i(General.TAG,Tag+"Page Loading is Started...");
        web.setVisibility(View.GONE);
        pbr.setVisibility(View.VISIBLE);

    }       

    @Override
    public boolean shouldOverrideUrlLoading(WebView  view, String url)
    {           
    view.loadUrl(url);  
    return true;            
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);            
        Log.i(General.TAG,Tag+"Page Loading is Finished");

        web.setVisibility(View.VISIBLE);
        pbr.setVisibility(View.GONE);
        web.requestFocus();
    }



}

@Eldhose M Babu and @ankitmakwana: I have used that, but I''m not sure what is missing in that. @Eldhose M Babu和@ankitmakwana:我曾经用过,但是我不确定那是什么。 I have attached the code, please have a look and guide me: 我已附上代码,请看一下并指导我:

mWebview.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);

            mWebview.setVisibility(View.GONE);


        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);

            mWebview.setVisibility(View.VISIBLE);

            mWebview.requestFocus();
        }

        public void onLoadResource(WebView view, String url) {
            mWebview.loadUrl("javascript:(function() { "
                    + "document.getElementsByTagName('header')[0].style.display = 'none'; "
                    + "})()");

            mWebview.loadUrl("javascript:(function() { "
                    + "document.getElementsByTagName('footer')[0].style.display = 'none'; "
                    + "})()");

            mWebview.loadUrl("javascript:(function() { "
                    + "document.getElementsByTagName('section').search_again.style.display = 'none'; "
                    + "})()");
        }
    });

    mWebview.loadUrl("http://search.yahoo.com/mobile/s?submit=oneSearch&.intl=us&.lang=en&.tsrc=yahoo&.sep=fp&x=0&y=0&p=bike");
    setContentView(mWebview);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM