dpdrtj1075 2013-05-20 06:30
浏览 57
已采纳

什么是最好的想法登录和注册android + php mysql(初学者)[关闭]

i'm expert in php , mysql programming... i write a php,mysql script that users can login, register, have profiles and so much more... i'm just a beginner in android programming... i write a android app that have a webview for show mobile version of my site..

i want know: how can i check the user logged / or not (sessions created with php when user login is success)? (really i want check for example $_SESSION['logged'] in my android app for show a specific activity (if session exsits, show activity1 , and if not exit show activity2)

important note : i'm a bigginer in android!

sorry for my English (English, is not my native language!)

my problem solved! You can use this tutorial ...... that is very very easy and understandable ... Android Login and Registration with PHP, MySQL and SQLite

  • 写回答

1条回答 默认 最新

  • dongyongyin5339 2013-05-20 06:44
    关注

    I am not a professional. Hope I can solve your problem. I have two method to do the member system.

    First, I will create a Data model like User.java

    -> User.java

    class User{
     private String username = "";
     private String password = "";
    
    // Getter and Setter..
    }
    

    while a user is login, create a new user object.

    User user = new User();
    user.setPassword(...);
    user.setUsername(...);
    

    When you need to get information from server by httprequest. What you need to do is get the information from User object

     Url url = new Url("http://your.server/index.php?username=" + user.getUsername() + "&password=" + user.getPassword());
     HttpURLRequest conn = url.openConnection();
     .......
    

    Further information. I usually save the cookie from the login process.

    ArrayList<String> cookiesList = new ArrayList<String>();
    Map<String, List<String>> mHeaderMap = mConnection.getHeaderFields();
    String headername = "";
    for(int i = 0; (headername = mConnection.getHeaderFieldKey(i)) != null; i++){
        if(headername.equals("Set-Cookie")){
            cookiesList.add(mConnection.getHeaderField(i));
        }
    }
    

    Then if you send another login request. you use the cookie again and send to the server.

    mConnection.setRequestProperty("Cookie", cookie);
    

    More Information here.

    On server side.

    <?php
    session_start();
    if($_GET.count() > 0)
    $username = $_GET['username'];
    if($_GET.count() > 0)
    $password = $_GET['password'];
    $arr = array();
    
    if($_SESSION['username'] == $username && $_SESSION['password'] == password){
     $arr = array("isValid" => true, "message" => "");
    }else{
     $arr = array("isValid" => false, "message" => "");
    }
    echo json_encode($arr);
    ?>
    

    On android

    if(isValid){
      startActicity(....)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀