如何检测412 precondition Android中的WebView URL失败的错误 [英] How to detect 412 precondition failed errors in android webview url

查看:113
本文介绍了如何检测412 precondition Android中的WebView URL失败的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是身份验证的WebView我app.There一个叫做相对于搜索词的URL,例如,如果你在搜索框中输入ABC,它会调用的URL<一href="http://something.com/something/">http://something.com/something/abc/something".它加载罚款,直到我有一个无效的搜索关键词说,如果我输入INVALIDCRAP它仍然会采取网址为<一个href="http://something.com/something/">http://something.com/something/INVALIDCRAP/something并尝试这样加载它给我412 precondition错误。我想让它这样的情况下标识符INVALIDCRAP犯规存在,而不是412 precondition错误就会重定向我说就怎么去做google.com。任何想法?提前致谢 这是我的$ C $下是相同的:

I am using an authenticated webview for my app.There is a url called with respect to a search term, such that if you entered abc in the search box, it will call the url "http://something.com/something/abc/something". It loads fine, until I have an invalid searchterm say if I type in "INVALIDCRAP" it will still take the url as "http://something.com/something/INVALIDCRAP/something and try to load it thus giving me the 412 precondition error. I want to make it such that in case the identifier "INVALIDCRAP" doesnt exist, instead of 412 precondition error it will redirect me to say "google.com".Any ideas on how to go about it ? Thanks in advance Here is my code for the same:

 @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            Bundle bundle = this.getArguments();
            URL = getUrl();

            if(bundle != null)
                mSearchTerm = getArguments().getString(SEARCH_TERM);

             }
     public void setSearchTerms(String sTerms){
         mSearchTerm = sTerms;
     }
private static String getUrl(){
        String url = "";

        final String getuuid = SharedPreferencesManager.getInstance().getUUID();
        final String environmentApi = SharedPreferencesManager.getInstance().getEnvironmentApi();
        if (environmentApi.equalsIgnoreCase(NetworkUtils.Apis.ALPHA)) {
            "https://something.com/service/v1/?q="+mSearchTerm+"%20revenue&ui.theme=novadark&uuid="+getuuid+"&userAgent=android";



        }


@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (goingBack) {
            return null;
        }
        final MainActivity activity = (MainActivity) getActivity();
        final Resources resources = Application.getAppResources();

        activity.setBackPressListener(this);
        View view = inflater.inflate(R.layout.fragment_search_answers, container, false);


        mWebview =  (WebView)view.findViewById(R.id.webview);
        progressBar = (LinearLayout) view.findViewById(R.id.loading);   
        mWebview.setVisibility(View.VISIBLE);

        progressBar.setVisibility(View.VISIBLE);

            mWebview.setWebViewClient(new MyWebViewClient(this, mWebview));

            mWebview.setBackgroundColor(0);
            mWebview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
            mWebview.getSettings().setAllowFileAccess(true);
            mWebview.getSettings().setDomStorageEnabled(true);
            mWebview.getSettings().setJavaScriptEnabled(true);

            mWebview.setHttpAuthUsernamePassword(HOST, REALM, USERNAME, PASSWORD);
            mWebview.setScrollbarFadingEnabled(false);
            mWebview.getSettings().getLoadsImagesAutomatically();


            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
            mWebview.getSettings().setDisplayZoomControls(false);
            if(StringUtils.isEmpty(URL)){

            }else if(!StringUtils.isEmpty(URL)){


                if(URLUtil.isValidUrl(URL)){
                     mWebview.loadUrl(URL);
                }else{
                    mWebview.loadUrl("http://www.google.com");


                }

            }



        return view;
    }

  public class MyWebViewClient extends WebViewClient {
        private String loginCookie;
        public MyWebViewClient(AnswersWebViewFragment answersWebViewFragment, WebView webview) {
            super();
        }
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            view.setVisibility(View.VISIBLE);

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            view.setVisibility(View.VISIBLE);
            CookieManager cookieManager = CookieManager.getInstance();
            cookieManager.setCookie(url, loginCookie);
            final Animation fade = new AlphaAnimation(0.0f, 1.0f);
            fade.setDuration(200);
            view.startAnimation(fade);
            view.setVisibility(View.VISIBLE);
            progressBar.setVisibility(View.GONE);

        }
        @Override
        public void onReceivedError( WebView view, int errorCode, String description, String failingUrl ) {
            Toast.makeText(view.getContext(), "Authentication Error", Toast.LENGTH_LONG).show();
            super.onReceivedError(view, errorCode, description, failingUrl);
        }

        @Override
        public void onLoadResource( WebView view, String url ){

        }
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

                return super.shouldOverrideUrlLoading(view, url);


        }


        @Override
        public void onReceivedHttpAuthRequest( WebView view, final HttpAuthHandler handler, final String host, final String realm ){
                handler.proceed(USERNAME,PASSWORD);

    }

         @Override
        public void onReceivedSslError( WebView view, SslErrorHandler handler, SslError error ) {
            handler.proceed();
        }

    }

这里的屏幕是一样的:

Here's the screen for the same:

推荐答案

如果真的是onReceivedError不叫话,我想,只有一个选项(也是唯一的条件下,你控制的网站)的情况。只是作一个假设,标题将包含如服务器错误,如果412发生。

If that really is the case that onReceivedError is not called then I suppose there's only one option (and only under condition that you control the website). Just make the assumption that the title will contain e.g "Server Error" if 412 happens.

...your code

webview.setWebChromeClient(new WebChromeClient() {
   @Override
   public void onReceivedTitle(WebView view, String title) {
      super.onReceivedTitle(view, title);

      String serverError = "ServerError";
      if (title.contains(serverError)) {
          view.stopLoading();
          webview.loadUrl(getNewUrl());
      }
   }
});

public String getNewUrl(){
   //do your stuff here
   return "http://www.google.com";
} 

这篇关于如何检测412 precondition Android中的WebView URL失败的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆