gpt4 book ai didi

java - 登录成功后如何在另一个 Activity (配置文件 Activity )中加载用户的数据库?我正在尝试创建一个个人资料页面

转载 作者:行者123 更新时间:2023-12-01 08:52:02 25 4
gpt4 key购买 nike

我已经创建了一个登录 Activity ,登录成功后它工作正常。 “个人资料” Activity 正在打开,但我想加载我存储在 mysql 数据库中的登录用户详细信息。谁能帮我怎么做?

我的Backgroundworker.java 文件

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://192.168.43.59/login.php";
String register_url = "http://192.168.43.59/register.php";
if (type.equals("login")) {
try {
String username = 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("user_name","UTF-8")+"="+URLEncoder.encode(username,"UTF-8")+"&"
+URLEncoder.encode("pass_word","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();
}
} else if (type.equals("register")){
try {
String name = params[1];
String surname = params[2];
String username = params[3];
String password = params[4];
String address = params[5];
String pinArea_code = params[6];
String email = params[7];
String phone = params[8];
URL url = new URL (register_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("reg_name","UTF-8")+"="+URLEncoder.encode(name,"UTF-8")+"&"
+URLEncoder.encode("reg_surname","UTF-8")+"="+URLEncoder.encode(surname,"UTF-8")+"&"
+URLEncoder.encode("reg_user","UTF-8")+"="+URLEncoder.encode(username,"UTF-8")+"&"
+URLEncoder.encode("reg_pass","UTF-8")+"="+URLEncoder.encode(password,"UTF-8")+"&"
+URLEncoder.encode("reg_add","UTF-8")+"="+URLEncoder.encode(address,"UTF-8")+"&"
+URLEncoder.encode("reg_pin","UTF-8")+"="+URLEncoder.encode(pinArea_code,"UTF-8")+"&"
+URLEncoder.encode("reg_mail","UTF-8")+"="+URLEncoder.encode(email,"UTF-8")+"&"
+URLEncoder.encode("reg_ph","UTF-8")+"="+URLEncoder.encode(phone,"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;
}

@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Login Status");
}

@Override
protected void onPostExecute(String result) {
alertDialog.setMessage(result);
alertDialog.show();
if(result.contains("login success")){
Intent ilogin = new Intent(context, Profile.class);
context.startActivity(ilogin);
alertDialog.dismiss();
} else if (result.contains("Account Created")){
Intent iregister = new Intent(context, about_us.class);
context.startActivity(iregister);
alertDialog.dismiss();
}

}

@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}

我的 MainActivity.Java 文件

package com.quickhomeservice.qhs.astudio;

import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
EditText UsernameET, PasswordET;
//String username, password;
private TextView button_reg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
UsernameET = (EditText)findViewById(R.id.editText_user);
PasswordET = (EditText)findViewById(R.id.editText_pass);
ActionBar ab = getSupportActionBar();
//ab.setLogo(R.mipmap.logo_mini86);
ab.setDisplayUseLogoEnabled(true);
ab.setDisplayShowHomeEnabled(true);
OnClickButtonListner();
}

public void OnLogin (View view){
String username = UsernameET.getText().toString();
String password = PasswordET.getText().toString();
String type = "login";

BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(type, username, password);
}

public void OnClickButtonListner(){
button_reg=(TextView) findViewById(R.id.register_link);
button_reg.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.quickhomeservice.qhs.astudio.registraion_form");
startActivity(intent);
}
}
);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_activity, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.Contact_id:
Intent intent1 = new Intent(this, contact_us.class);
startActivity(intent1);
return true;
case R.id.About_id:
Intent intent2 = new Intent(this, about_us.class);
startActivity(intent2);
return true;
}

return super.onOptionsItemSelected(item);
}
}

还有我的login.php 文件

<?php
require "conn.php";
$user_name = $_POST["user_name"];
$user_pass = $_POST["pass_word"];
$mysql_qry = "select * from user_data where username like '$user_name' and password like '$user_pass';";
$result = mysqli_query($conn ,$mysql_qry);
if(mysqli_num_rows($result) > 0){
echo "login success";
}
else{
echo "login failed";
}
?>

最佳答案

在您的 php 文件中,您可以将查询结果回显为 JSON,而不仅仅是回显“登录成功”,因为您的查询会选择表中与给定条件匹配的所有列。

PHP 示例:

<?php

require "conn.php";

$user = $_POST["username"];
$password = $_POST["password"];


$response["user_data"] = array();

//Use mysqli_prepare to secure yourself from sql injection.

$query = $conn->prepare("Select * from user_data where username = ? and password = ?");
$query->bind_param("ss", $user, $password);
$query->execute();

$result = $query->get_result();

// check if the query returned a row (which means success)
if(mysqli_num_rows($result) > 0){

$response["message"] = "Log in success";

$getUserData = $conn->prepare(// your query here where username = ?);
$getUserData->bind_param("s", $user);
$getUserData->execute();

$getResult = $getUserData->get_result();

while($row_data = $getResult->fetch_assoc()){

array_push($response["user_data"] = $row_data);

}

echo json_encode($response);


}
else{

// your else statement.

}

?>

并用java解析它。

关于java - 登录成功后如何在另一个 Activity (配置文件 Activity )中加载用户的数据库?我正在尝试创建一个个人资料页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42326910/

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