我正在为我的应用使用经过身份验证的网页浏览。有一个关于搜索字词的网址,这样如果您在搜索框中输入了abc,则会调用网址“http://something.com/something/ ABC /东西”。它加载正常,直到我有一个无效的searchterm,如果我输入“INVALIDCRAP”,它仍然会将网址设为“http://something.com/something/ INVALIDCRAP /某事,并尝试加载它,从而给我一个412前提条件错误。我想这样做,以防标识符“INVALIDCRAP”不存在,而不是412前提条件错误,它将重定向我说“google.com”。任何关于如何去做的想法?提前谢谢 这是我的相同代码:
@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();
}
}
这是相同的屏幕:
答案 0 :(得分:1)
如果确实没有调用onReceivedError,那么我认为只有一个选项(并且只有在您控制网站的情况下)。如果412发生,只要假设标题将包含例如“服务器错误”。
...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";
}