gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 18:16:56 25 4
gpt4 key购买 nike

我在android中很新。在我的应用程序中,我希望当用户登录时,该用户的id携带所有 Activity 。就像 session 管理一样。该用户id来自mysql数据库。我想将登录用户id携带到所有选项卡它显示在主要 Activity 中。我尝试了全局变量,但它不起作用。请给我一些解决方案......

登录 Activity

@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 携带到所有 Activity 中。

最佳答案

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

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

SharedPreverences doc

关于php - 如何在我的 Android 应用程序中使用全局变量从 mysql 检索用户的登录 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46938800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com