3

我是安卓新手。我正在为移动设备开发 android 应用程序。当用户第一次在移动设备上使用我的应用程序时,我想在服务器端保存用户名和密码,并在我的网站上使用用户名和密码验证该用户名和密码。如果用户没有在我的网站上注册立即注册按钮将控制重定向到我的网站。我不知道如何实现这一点。我知道这不是一个好问题。但我有麻烦了。请有人帮助我。

提前致谢...

如果有人有一些代码来解释我,我非常感谢......

4

2 回答 2

2

本教程将引导您了解有关创建注册/登录身份验证的所有信息。

于 2013-09-20T13:13:28.073 回答
1

使用 OAuth 将用户名和密码安全地发送到您的服务器并接收回复......因为 Facebook、Google、Twitter 正在使用 OAuth 来验证用户......

OAUth 1.0 - http://oauth.net/
OAuth 2.0 - http://oauth.net/2/

OAuth 2.0 教程

http://tutorials.jenkov.com/oauth2/index.html

http://net.tutsplus.com/tag/oauth-2-0/

或者

您可以使用以下步骤通过对 PHP 服务器的 HTTP 发布请求安全地验证您的 android 应用程序中的用户。

  1. 创建 HTTP Post 请求并将带有 HTTP 请求的用户名和密码发送到您的服务器

     public void postData() {
       // Create a new HttpClient and Post Header
       HttpClient httpclient = new DefaultHttpClient();
       HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
    
       try {
          // Add your data
          List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
          nameValuePairs.add(new BasicNameValuePair("username", "12345"));
          nameValuePairs.add(new BasicNameValuePair("password", "12345676890"));
          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    
        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        BufferedReader reader = new BufferedReader(new      InputStreamReader(response.getEntity().getContent(), "UTF-8"));
        StringBuilder builder = new StringBuilder();
        for (String line = null; (line = reader.readLine()) != null;) {
            builder.append(line).append("\n");
        }
        builder.toString();
        //Use builder.toString() to see output
    
        } catch (ClientProtocolException e) {
          // TODO Auto-generated catch block
        } catch (IOException e) {
        // TODO Auto-generated catch block
      }
    } 
    
  2. 在服务器上的 script.php 文件中接收用户名和密码

    下面的代码是用于 mysql 数据库 -

    if(isset($_POST)){
       $username = $_POST['username'];
       $password = $_POST['password'];
    
    //Connect to your database & check if above values exists in your user database or not     
    $host="localhost"; // Host name 
    $username="root"; // Mysql username 
    $password=""; // Mysql password 
    $db_name="test"; // Database name 
    $tbl_name="members"; // Table name 
    
    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
    
    
      // To protect MySQL injection (more detail about MySQL injection)
      $username = stripslashes($myusername);
      $password = stripslashes($mypassword);
      $username = mysql_real_escape_string($username);
      $password = mysql_real_escape_string($password);
      $sql="SELECT * FROM $tbl_name WHERE username='$username' and     password='$password'";
      $result=mysql_query($sql);
    
      // Mysql_num_row is counting table row
      $count=mysql_num_rows($result);
    
      // If result matched $username and $password, table row must be 1 row
      if($count==1){
    
       //echo back the success string with code in JSON or any other format
       echo "SUCCESS:User is registered";
      }
      else {
         echo "FAILURE:Wrong Username or Password";
         }
      }
    
  3. 在您的 Android Activity 中解析 HTTP 请求的响应并根据响应检查用户是否已注册

    Parse response from this line..
    HttpResponse response = httpclient.execute(httppost);
    
    BufferedReader reader = new BufferedReader(new      InputStreamReader(response.getEntity().getContent(), "UTF-8"));
    StringBuilder builder = new StringBuilder();
    for (String line = null; (line = reader.readLine()) != null;) {
         builder.append(line).append("\n");
    }
    builder.toString();
     //Use builder.toString() to see output
    
于 2013-09-20T13:13:57.920 回答