从android webview中的网页重定向

时间:2015-06-28 07:29:33

标签: javascript android html webview url-redirection

所以我有这个表格。我想在我的网页浏览中加入。

    <html>
    <head>
    <meta name="viewport" content="user-scalable = yes">

    <link rel="stylesheet" href="assets/css/bootstrap.css">
    </head>
    <body>
           <!--LOGIN FORM -->
    <div class="border" Style="width:100%;max-width:100%;height:100%;">
    <div class="bs-example form-horizontal">
    <div id="msg-info" style="display:none;" ></div>
      <form class="form-horizontal" id="login-user" name="login-user" action='' method="POST">

        <fieldset>

        <div id="legend">

            <legend class="">Reigstered Members Login<span class="text-center">&nbsp;&nbsp;&nbsp;<img id="spinner" src="images/loading.gif" style="display:none;"/></span></legend>

        </div>

        <div class="control-group">

            <!-- Username -->

            <label class="control-label" for="username" style="text-align:left;"><strong>User ID</strong>&nbsp;<small>(It can be Account No, Mobile No or Email)</small></label>

            <div class="controls">

            <input type="text" id="username" name="username" placeholder="" class="form-control">

            </div>

            <!-- Password-->

            <label class="control-label" for="password" style="text-align:left;"><strong>Password</strong></label>

            <div class="controls">

            <input type="password" id="password" name="password" placeholder="" class="form-control">

            </div>

            <label class="control-label" for="password">&nbsp;</label>
            <!-- Button -->

            <div class="controls"> 
                <input type="button" id="login-btn" name="login" class="btn btn-success" value="Login &raquo;" />&nbsp;&nbsp;&nbsp;&nbsp;
            </div>
          </div>  
         </fieldset>
        </form>
      </div>

            <p><font color="red">Note:</font> Those of you who have not yet registerd can register from here, Note you have to update your mobile no with the society so that registration can be completed</p> 


      </div>
    <br><br><br><br>
<script>
$("#login-btn").click(function(){
    validateForm();
});
$("#login-user").keypress(function(event){
    if(event.keyCode == 13  || event.which == 13){
        validateForm();
    }
});
function validateForm(){
    var flag=true;
    if($("#username").val()==''){
        $("#msg-info").addClass("alert alert-danger");
        $("#msg-info").html('Enter valid User ID.');
        $("#msg-info").show("slow");    
        flag=false;
        return false;
    }
    if($("#password").val()==''){   
        $("#msg-info").addClass("alert alert-danger");
        $("#msg-info").html('Enter valid Password.');
        $("#msg-info").show("slow");
        flag=false;
    }
    if(flag){
        $("#msg-info").hide("slow");
        //
        $('#spinner').show();
        jQuery.post('validations/loginStore.php', $('form[name=login-user]').serialize(), function(data) {
            data=JSON.parse(data);
            $("#msg-info").removeClass("alert-danger");
            if(data.update){
                //redirect to secure page
                if(data.role=='admin'){
                    window.open('app/dashboard.php?page=societylist&profile=dash', '_parent');
                } else {
                    window.open('app/dashboard.php?page=home', '_parent');
                }
            } else {
                $("#msg-info").addClass("alert alert-danger");
            }
            if(typeof(data.msg)!='undefined' && data.msg !='' && !data.update){

                    $("#msg-info").html(data.msg);
                }
           $("#msg-info").show();
           $('#spinner').hide();
        });
    }
}
</script>
        </body>
         </head>

这在网络上运行得很好但是当我尝试在我的网页浏览中加载它时,重定向不会发生。我能用这种方式做到吗?我也在下面发布我的android代码。 webview加载正常..唯一的问题是重定向

Android代码

public class LoginActivity extends ActionBarActivity {

WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    myWebView = (WebView) findViewById(R.id.webView);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    webSettings.setLoadsImagesAutomatically(true);
    webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

    myWebView.loadUrl("http://www.chsonline.in/api/login-frame.php");



    myWebView.setWebViewClient(new WebViewClient());
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the Back button and if there's history
    if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
        myWebView.goBack();
        return true;
    }
    // If it wasn't the Back key or there's no web page history, bubble up to the default
    // system behavior (probably exit the activity)
    return super.onKeyDown(keyCode, event);
}

}

请帮助我解决我做错了什么或者我错过了什么

2 个答案:

答案 0 :(得分:0)

您似乎遇到了以下链接相关的问题。 1. Opening webview not in new browser 2. parent.opener doesn't work in webview Android 这可能会对你有帮助。

答案 1 :(得分:0)

尝试使用

myWebView.getSettings().setPluginState(PluginState.ON); // this is for newer API's

此外,如果您想强制重定向到特定页面,请覆盖shouldOverrideUrlLoading()方法。

@Override
    myWebView.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
       }
});

这将在加载网页后立即加载网址。您还可以使用计时器显示静态网页,然后将其重定向到新页面。