WebView: Bad request while loading url

When I try to load the url in the 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 am getting this html in the page:

Bad request

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

enter image description here

There onReceivedError(WebView view, int errorCode, String description, String failingUrl)

is nothing in the callback .

+3


source to share


5 answers


It is giving you this error because you are not handling redirects from this webpage incorrectly 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
 }
});

      

+2


source


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

 }
}       

      

+2


source


Try to change

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

      

it is for

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

      

Hope this helps 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.

+1


source


It is always best to maintain the API level number when this issue occurs.

For example, in API 19, the shouldOverrideUrlLoading function cannot trigger validation, http://developer.android.com/guide/webapps/migrating.html

Note: shouldOverrideUrlLoading is not called when the url is loaded, like

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

and also Redirect does not work at API level 11 and below. (As you answered Abdul it is not called)

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

Strictly Charah around:

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

+1


source


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

      

Then you need to build a layout like 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 it helps :)

0


source







All Articles