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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料