繁体   English   中英

如何在我的 android 应用程序中使用全局变量从 mysql 检索用户的登录 ID?

[英]how can I retrieve user's login id from mysql using global variable in my android app?

我在android中很新。在我的应用程序中,我希望在用户登录时携带所有活动。就像会话管理一样。那个用户ID来自mysql数据库。我想将登录用户ID携带到显示在的所有选项卡中主要活动。我尝试了全局变量,但没有用。请给我一些解决方案....

登录活动

@Override
public void onClick(View view) {

    // Get text from email and passord field
    String email = UsernameEt.getText().toString();
    String password = PasswordEt.getText().toString();
    String type = "login";
    BackgroundWorker backgroundWorker = new BackgroundWorker(this);
    backgroundWorker.execute(type, email, password);
    //finish();
    Intent intent = new Intent(UserLogin.this, MainActivity.class);
    startActivity(intent);
    finish();

}

BackgroundWorker 类

public class BackgroundWorker extends AsyncTask<String,Void,String> {

Context context;
AlertDialog alertDialog;


BackgroundWorker (Context ctx){

    context = ctx;

}

@Override
protected String doInBackground(String... params) {

    String type = params[0];
    String login_url = "http://www.mybusket.com/pmsapp/webs/get_all_emp.php";
    if (type.equals("login")){

        try {
            String email_id = params[1];
            String password = params[2];
            URL url = new URL(login_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
            String post_data = URLEncoder.encode("email_id", "UTF-8")+"="+URLEncoder.encode(email_id, "UTF-8")+"&"
                    +URLEncoder.encode("password", "UTF-8")+"="+URLEncoder.encode(password, "UTF-8");
            bufferedWriter.write(post_data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
            String result="";
            String line="";
            while ((line = bufferedReader.readLine())!= null){
                result += line;
            }
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return result;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    return null;
}

从这个类验证电子邮件地址..如何将保存在 mysql 数据库中的登录用户 ID 带到所有活动。

Android 应用程序不是网络应用程序,因此您无需“携带”任何东西。

如果您需要存储要与不同活动共享的 userId,您可以将其放在 SharedPreferences 中。

SharedPreverences 文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM