WebView:加载网址时出现错误请求

时间:2014-11-07 06:37:17

标签: android webview

当我尝试加载URL时,在WebView中:

private final WebViewClient webViewClient = new WebViewClient() {
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        loadUrl(url);
        return true;
    }
};
webView.setWebViewClient(webViewClient);
webView.loadUrl("https://post.craigslist.org/");

我在页面上看到了这个HTML:

  

错误请求

     

您的浏览器发送了此服务器无法理解的请求。

enter image description here

onReceivedError(WebView view, int errorCode, String description, String failingUrl)回调中也没有任何内容。

6 个答案:

答案 0 :(得分:2)

由于您未从此网页正确处理重定向,因此它会显示此错误 试试这个

webview.setWebViewClient(new WebViewClient() {
  public boolean shouldOverrideUrlLoading(WebView view, String url){
    // do your handling codes here, which url is the requested url
    // probably you need to open that url rather than redirect:
    view.loadUrl(url);
    return false; // then it is not handled by default action
 }
});

答案 1 :(得分:2)

试试这样:

public class WebPageLoader extends Activity
 {

   final Activity activity = this;
   private String html;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
      super.onCreate(savedInstanceState);
      this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
      setContentView(R.layout.activity_main);
      WebView webView = (WebView) findViewById(R.id.webview);
      webView.getSettings().setJavaScriptEnabled(true);

webview.loadUrl("https://post.craigslist.org/");

       webView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress)
        {
            activity.setTitle("Loading...");
            activity.setProgress(progress * 100);

            if(progress == 100)
                activity.setTitle(R.string.app_name);
        }
     });

     webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
        {

        }

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

 }
}       

答案 2 :(得分:1)

尝试更改

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

这个

public boolean shouldOverrideUrlLoading(WebView view, String url) {
    return super.shouldOverrideUrlLoading(view, url);
}

我希望这会对你有所帮助。

<强>更新

mWebView = (WebView) getView().findViewById(R.id.webView1);
if (Patterns.WEB_URL.matcher(url).matches())
        mWebView.loadUrl(url);      

webClient = new WebViewClient() {       

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

    @Override
    public void onFormResubmission(WebView view, Message dontResend,
            Message resend) {
        // TODO Auto-generated method stub
        super.onFormResubmission(view, dontResend, resend);
    }

    @Override
    public void onLoadResource(WebView view, String url) {
        super.onLoadResource(view, url);
        if (url.contains("purchasehistory.html")) {
            mURLNavigation.onURLNavigation(3);
        }
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }
};

WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.setWebViewClient(webClient);

此代码适用于我..

答案 3 :(得分:1)

当遇到此类问题时,最好保留API级别编号。

例如,在API 19中,函数shouldOverrideUrlLoading无法调用此方法,http://developer.android.com/guide/webapps/migrating.html

注意: 加载像

这样的网址时,不会调用shouldOverrideUrlLoading

loadUrl(&#34; http://google.com&#34;)

并且Redirect也不适用于API Level 11及更低版本。 (当你回复阿卜杜勒时,它没有被召唤)

使用变通方法:我的意思是覆盖onPageStarted,它适用于所有版本。但是,唯一的事情就是它稍后被称为。

严格随机解决:

在Android 4.1.1上使用WebView.getSettings()。setUseWideViewPort(true),它可以正常工作。

答案 4 :(得分:0)

这是您在WebView中加载网址的方式:

private WebView URL

URL = (WebView) WebDialog.findViewById(R.id.url1);  
URL.setWebViewClient(new WebViewClient());
URL.setScrollbarFadingEnabled(true);  
URL.setHorizontalScrollBarEnabled(false);  
URL.getSettings().setJavaScriptEnabled(true);
URL.getSettings().setUserAgentString("My URL");  
URL.loadUrl("//the first url goes here");

然后,您需要构建一个与此类似的布局作为xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout

        <WebView
            android:id="@+id/ticketline"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@id/Title"
            android:layout_marginTop="5dip" />
    </RelativeLayout>

</LinearLayout>

希望这会有所帮助:)

答案 5 :(得分:0)

        webview.loadUrl("http://route.izooto.com/?id=1563543&client=41882&rid=4188273230385&frwd=aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbT91dG1fc291cmNlPWl6b290byZ1dG1fbWVkaXVtPXB1c2hfbm90aWZpY2F0aW9ucyZ1dG1fY2FtcGFpZ249VGVzdCBpWm9vdG8gTFAmdXRtX2NvbnRlbnQ9JnV0bV90ZXJtPQ==&bkey=f9_DIVuweYA:APA91bFE9x7PN6J5Oy67NypwlT8ZT3Qgz0VSYtCT4_DH5v4l7mCsY7q3aqSrOh6mCcWKIjz4aRiOWQ8ap85r85vHiTh3Mc8n4MiHSnbMXUKGB_5HkDjiBCQg5a7IQifvrakl7bIXF_oA");
       WebViewClient webClient = new WebViewClient() {

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

            @Override
            public void onFormResubmission(WebView view, Message dontResend,
                                           Message resend) {
                // TODO Auto-generated method stub
                super.onFormResubmission(view, dontResend, resend);
            }

            @Override
            public void onLoadResource(WebView view, String url) {
                super.onLoadResource(view, url);
                if (url.contains("purchasehistory.html")) {
                   // mURLNavigation.onURLNavigation(3);
                }
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }
        };

        WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webview.setWebViewClient(webClient);