2015-06-28 61 views
1

所以我有這種形式與我。我想在我的webview中加入。從android webview中的網頁重定向

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

這工作完全正常在網絡上,但是當我嘗試加載它在我的webview,重定向不會發生。任何方式,我可以做到這一點?我也在下面發佈我的android代碼。 web視圖加載罰款..唯一的問題是與重定向

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

} 

請幫助我什麼我做錯了,或者如果我失去了一些東西

+0

http://stackoverflow.com/questions/12758078/android-webview-cangoback-error –

回答

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

這會在加載網頁後立即加載url。您還可以使用計時器來顯示靜態網頁,然後將其重定向到新頁面。