2017-02-09 54 views
0

這是我迄今所做的:我如何可以驗證在xamarin用戶登錄信息存儲在服務器WAMP

這是我MainActivity.cs:

using Android.App; 
using Android.Widget; 
using Android.OS; 
using Android.Views.InputMethods; 
using Android.Content; 
using Android.Views; 

namespace App 
{ 
    [Activity(Label = "App", MainLauncher = true, Icon = "@drawable/icon")] 
    public class MainActivity : Activity 
    { 
     protected override void OnCreate(Bundle bundle) 
     { 

      base.OnCreate(bundle); 
      SetContentView (Resource.Layout.Main); 
      Button Login = (Button)FindViewById(Resource.Id.button1); 
      Login.Click += Login_Click; 

     } 

     private void Login_Click(object sender, System.EventArgs e) 
     { 
      EditText username = (EditText)FindViewById(Resource.Id.editText1); 
      EditText password = (EditText)FindViewById(Resource.Id.editText2); 
      Login(username.Text, password.Text); 

      //Intent Poziv = new Intent(this, typeof(AFLogcs)); 
      //this.StartActivity(Poziv); 
      //this.Finish(); 
     } 

     private void Login(string username, string password) 
     { 

     } 

     public override bool OnTouchEvent(MotionEvent e) 
     { 
      InputMethodManager imm = (InputMethodManager)GetSystemService(Context.InputMethodService); 
      EditText username = (EditText)FindViewById(Resource.Id.editText1); 
      EditText password = (EditText)FindViewById(Resource.Id.editText2); 
      //Cisti fokus 
      username.ClearFocus(); 
      password.ClearFocus(); 
      imm.HideSoftInputFromWindow(username.WindowToken, 0); 
      return base.OnTouchEvent(e); 
     } 

    } 
} 

我也創建了三個PHP文件在我的c:/ wamp64/www/android文件夾中連接到wamp服務器上的數據庫。

這裏的文件:

Conn.php

<?php 
$db_name = "korisnici"; 
$mysql_username = "root"; 
$mysql_password = ""; 
$server_name = "localhost"; 
$conn = mysqli_connect($server_name, $mysql_username, $mysql_password,$db_name); 
if($conn){ 
echo "Connection sucessfull"; 
} 
else{ 
echo "No connection"; 
} 
?> 

而且login.php中

<?php 
require "Conn.php"; 
$user_name = "Mpro"; 
$user_pass = "nice"; 
$mysql_qry = "SELECT* FROM korisnici WHERE korisnik LIKE '$user_name' and sifra LIKE '$user_pass'"; 
$result = mysqli_query($conn, $mysql_qry); 
if(mysqli_num_rows($result) > 0){ 
echo "Uspjesan login"; 
} 
else{ 
echo "login not success"; 
} 

現在我的問題是我如何可以傳遞的Android editTexts數據,用戶名和密碼並進行身份驗證以查看用戶是否存在。如果用戶存在,則新活動將被拉起,並且將顯示該特定用戶的另一個表中的用戶信息。

此外,一旦獲取數據,用戶將能夠離線登錄到他的應用程序,仍然可以看到記憶的數據,但再次連接到WiFi後,應用程序將自動檢查是否有更新,如果是應用程序將它從Wamp中拉出來並再次存儲它。

您不必共享數據存儲的代碼,但請用戶登錄。先謝謝你。

回答

1

我也使用Web服務登錄系統。我使用了Xamarin android(MvvmCross),它使用了ViewModelNode JS Web服務。因此,道歉我不能幫你很多的代碼傳遞給另一個活動和登錄php

但是,我能夠分享我是如何做到的。首先,我使用WebRequest類將用戶名和密碼傳遞給Web服務。你應該檢查一下。然後,Web服務會回覆登錄憑證是對還是錯,Android應用程序可以使用StreamReader來讀取響應。你可以檢查文檔WebRequesthere

要保存用於離線目的的登錄,我建議您使用的是ISharedPreferences類。它會將數據保存在持久存儲器中。可以在那裏保存的數據是原始數據,如int,boolean,string,floatstringset。使用它,您可以存儲boolean以檢查用戶是否已經登錄(您需要在用戶註銷後再刪除),用戶名和您需要的其他數據。檢查它在this thread

希望這可能有所幫助。祝你的項目好運。

+0

謝謝Darren我會盡我所能。 – SWALLOW