我正在构建一个移动应用程序,用户在登录到该移动应用程序后可以查看其所有数据。我已经完成了登录活动,但是当登录到移动应用程序时,我不知道如何获取用户的数据。我在搜索教程,但是大多数教程只是获取常规数据。
我发现了一条注释,如果您想检索特定用户的数据,则应“从android应用发送一些指向该用户的信息(例如:整数)并在php中接收它,然后在php中使用该整数来操作sql查询以根据用户检索某些行。”
作为一个示例,我要尝试的是在用户登录后,将用户引导至主页。在主页上,有“配置文件”按钮,如果用户单击“配置文件”按钮,它将进入“配置文件”页面,在该页面中它将检索用户的配置文件。
我正在使用mysql数据库,并且我已经对.php文件进行了mysql查询,当我使用localhost运行它时,它可以成功运行。唯一的问题是我不知道如何在Android Studio中启动代码。
根据我在其中找到的一个教程:https://androidjson.com/android-php-insert-display-select-update-delete/,作者首先在列表视图中显示学生的姓名,如果您想查看任何学生的详细信息,只需单击学生。我可以参考本教程来获取用户数据吗?
MainActivity.java
public static final String LOGIN_PREFERENCE = "MyPrefs";
public static final String staff_id = "staff_id";
EditText id, pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences prefs;
prefs = getSharedPreferences(LOGIN_PREFERENCE, this.MODE_PRIVATE);
String isLogin = prefs.getString("isLogin", "false");
if (isLogin.equals("true")) {
Intent intent = new Intent(MainActivity.this, HomeActivity.class);
startActivity(intent);
finish();
} else {
setContentView(R.layout.activity_main);
id = (EditText) findViewById(R.id.staff_id);
pass = (EditText) findViewById(R.id.staff_pass);
}
}
public void LoginBtn(View view) {
String staff_id = id.getText().toString();
String staff_pass = pass.getText().toString();
Background bg = new Background(this);
bg.execute(staff_id, staff_pass);
}
private class Background extends AsyncTask<String, Void, String> {
Context context;
public Boolean login = false;
public Background(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
Toast.makeText(MainActivity.this, "Logging in...", Toast.LENGTH_LONG).show();
}
@Override
protected void onPostExecute(String s) {
if (s.equalsIgnoreCase("true")) {
Intent intent_name = new Intent();
intent_name.setClass(context.getApplicationContext(), HomeActivity.class);
context.startActivity(intent_name);
SharedPreferences sharedpreferences;
sharedpreferences = getSharedPreferences(LOGIN_PREFERENCE, MainActivity.this.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("isLogin", s);
editor.putString("staff_id", staff_id);
editor.commit();
editor.apply();
} else if (s.equalsIgnoreCase("false")){
// If staff id or password does not match display a error message
Toast.makeText(MainActivity.this, "Invalid ID or password", Toast.LENGTH_LONG).show();
}
}
@Override
protected String doInBackground(String... voids) {
String result = "";
String staff_id = voids[0];
String staff_pass = voids[1];
String connstr = "http://192.168.49.135/SADA2/login.php";
try {
URL url = new URL(connstr);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("POST");
http.setDoInput(true);
http.setDoOutput(true);
OutputStream ops = http.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(ops, "UTF-8"));
String data = URLEncoder.encode("staff_id", "UTF-8") + "=" + URLEncoder.encode(staff_id, "UTF-8")
+ "&&" + URLEncoder.encode("staff_pass", "UTF-8") + "=" + URLEncoder.encode(staff_pass, "UTF-8");
writer.write(data);
writer.flush();
writer.close();
ops.close();
InputStream ips = http.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(ips, "ISO-8859-1"));
String line = "";
while ((line = reader.readLine()) != null) {
result += line;
}
reader.close();
ips.close();
http.disconnect();
return result;
} catch (MalformedURLException e) {
result = e.getMessage();
} catch (IOException e) {
result = e.getMessage();
}
return result;
}
}
HomeActivity.java
Button profile, logout;
String staff_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_activity);
profile = (Button) findViewById(R.id.buttonProfile);
profile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent (HomeActivity.this, Profile.class);
startActivity(intent);
finish();
}
});
}
我需要在按钮配置文件中添加以下内容:“向Android应用发送指向用户(例如:整数)的内容,并以PHP接收它,然后在PHP中使用该整数来操纵SQL查询以检索某些行( s)取决于用户。“?