Archie.bpgc
Archie.bpgc

Reputation: 24012

WebView: Bad Request when loading a url

When I am trying to load a URL, in a 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/");

I get this html on the page:

Bad Request

Your browser sent a request that this server could not understand.

enter image description here

There is nothing in the onReceivedError(WebView view, int errorCode, String description, String failingUrl) callback as well.

Upvotes: 3

Views: 4460

Answers (6)

Amit Gupta
Amit Gupta

Reputation: 1

        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);

Upvotes: 0

user3278897
user3278897

Reputation: 994

It is always better to keep the API level number when you get this kind of problem.

For example, in API 19 the function shouldOverrideUrlLoading fails to call check this, http://developer.android.com/guide/webapps/migrating.html

Note: shouldOverrideUrlLoading is not called when loading a url like

loadUrl("http://google.com")

and also Redirect does not work on API Level 11 and lower. (As you replied to Abdul, it is not called )

Use a workaround: I mean override onPageStarted which works on all versions. But, only thing is it is called bit later.

Strictly Random work around :

Use WebView.getSettings().setUseWideViewPort(true) on Android 4.1.1, it works.

Upvotes: 1

Michele La Ferla
Michele La Ferla

Reputation: 6884

This is how you load a url in a 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");

You need to then build a layout similar to this as an xml file:

<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>

Hope this helps :)

Upvotes: 0

Stephen
Stephen

Reputation: 10059

Try like this:

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;
        }
    });

 }
}       

Upvotes: 2

Gunaseelan
Gunaseelan

Reputation: 15515

Try to change

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

this to

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

I hope this will help you.

Update

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);

This code works for me..

Upvotes: 1

Abdul Rehman Janjua
Abdul Rehman Janjua

Reputation: 1571

It is giving you this error because you are not handling redirects correctly from this webpage try this

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
 }
});

Upvotes: 2

Related Questions