首页 > 解决方案 > 我正在开发一个用于求职的 android 应用程序。这里我使用 PHP 作为后端

问题描述

在 php 代码中,当我们注册时会自动创建一个随机代码作为用户 ID。在 android 应用程序中,当我们保存个人资料信息时,我不能用户 ID 会有所不同。那么如何在所有活动中管理用户 ID 和片段保持相同的用户 ID 直到注销帮助我...................... ...... **

注册码:

<?php
 session_start();

   if($_SERVER['REQUEST_METHOD']=='POST'){

       include_once("db_connect.php");


    $username = $_POST['user_name'];
    $useremail = $_POST['user_email'];
    $usermobile = $_POST['user_mobile'];
    $password = $_POST['password'];



 function randomstring($len) {
    $string = "";
    $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    for($i=0;$i<$len;$i++)
        $string.=substr($chars,rand(0,strlen($chars)),1);
    return $string;
    }
    $rndm_code=randomstring(5);




$CheckSQL = "SELECT * FROM user WHERE user_email='$useremail'";
    $check = mysqli_fetch_array(mysqli_query($con,$CheckSQL));

          if(isset($check)){

         echo 'Email Already Exist';

          }

 else{


  $Sql_Query = "insert into user (user_id,user_name,user_email,user_mobile,password) values ('$rndm_code','$username','$useremail','$usermobile','$password')";


   if (mysqli_query($con,$Sql_Query)){



  echo 'User Registration Successfully';

 }
 else
 {

 echo 'Try Again';
 }

 }
 }
 mysqli_close($con);

 ?>

安卓代码:

 private void registerUser() {
             isConnectingToInternet();
            final String username = signupname.getText().toString().trim();
            final String email = signupemail.getText().toString().trim();
            final String phone = signupphone.getText().toString().trim();
            final String password = signuppassword.getText().toString().trim();


            pDialog = new ProgressDialog(SignupActivity.this);
            pDialog.setMessage("Signing Up.. Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();


            StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.URL_REGISTER,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String ServerResponse) {

                            // Hiding the progress dialog after all task complete.
                            pDialog.dismiss();

                            // Showing response message coming from server.
                            Toast.makeText(SignupActivity.this, ServerResponse, Toast.LENGTH_LONG).show();


                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError volleyError) {

                            // Hiding the progress dialog after all task complete.
                            pDialog.dismiss();

                            // Showing error message if something goes wrong.
                            Toast.makeText(SignupActivity.this,"Check Internet connection", Toast.LENGTH_LONG).show();
                        }
                    }) {
                @Override
                protected Map<String, String> getParams() {

                    // Creating Map String Params.
                    Map<String, String> params = new HashMap<String, String>();

                    // Adding All values to Params.
                    params.put("user_name",username);
                    params.put("user_email",email);
                    params.put("user_mobile", phone);
                    params.put("password",password);

                    return params;
                }

            };


            RequestQueue requestQueue = Volley.newRequestQueue(SignupActivity.this);


            requestQueue.add(stringRequest);




        }**

标签: phpandroid

解决方案


除了响应文本之外,您的服务器响应还需要包含 id。通常,这是通过将响应格式化为 JSON 来完成的。

PHP 脚本的响应可能不仅仅是“用户注册成功”,而是

{
  "message":"User Registration Successfully",
  "userId":<your_user_id_goes_here>
}

您可以在 PHP 中使用 json_encode 从数组中为您创建这种格式。代码看起来像这样

echo json_encode([
   "message" => "User Registration Successfully",
   "userId" => $rndm_code
]);
exit();

发送响应后退出会很好,这样您的 JSON deos 就不会因为您稍后不小心在某处回显更多内容而变得无效。

在 android 应用程序中,您将从 JSON 获取数据 - 我在 android 方面没有经验,但我认为您可以通过将其设为 JsonObjectRequest 而不是 StringRequest,然后显示 serverResponse.getString("message")并将 serverResponse.getString("userId") 作为 id 保存在某处的变量中。


推荐阅读