在android中将查询数据从一个活动传递到另一个活动

问题描述 投票:0回答:2

我是Android Studio编程的新手。

我使用phpms sql将我的应用程序连接到数据库。

我的PHP代码:

if( !$conn ) 

{

 echo "Connection could not be established.";

 die( print_r( sqlsrv_errors(), true));
}

else{
$user_name = $_POST["username"];
$pass_word = $_POST["password"];

$sql = "select emp_role,emp_id from employee where emp_loginid =(?) and 
emp_password =(?)";
$params = array($user_name,$pass_word);
$options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
$stmt = sqlsrv_query( $conn, $sql , $params, $options );

$row_count = sqlsrv_num_rows( $stmt );

if ($row_count > 0)
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ){
    echo $row["emp_role"];
    echo $row["emp_id"];
}
else
echo "username or password is incorrect";
}

这是我的backgroundwroker代码

public class BackgroundWorker extends AsyncTask<String,Void,String> {
Context context;
AlertDialog alertDialog;
String username, password;

BackgroundWorker(Context ctx) {

    context = ctx;
}

@Override
protected String doInBackground(String... params) {
    String type = params[0];
    String login_url = "http://localhost:8080/sqlsrv.php";
    if (type.equals("login")) {
        try {
            username = (String) params[1];
            password = (String) 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( "username", "UTF-8" ) + "=" + URLEncoder.encode( username, "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;
 }
 @Override
 protected void onPreExecute() {
    alertDialog = new AlertDialog.Builder(context).create();
    alertDialog.setTitle("Login Status");
 }

 @Override
 protected void onPostExecute(String result) {
    /*if(result != null){
        Intent intent=new Intent(context,LoginSucess.class);
        context.startActivity(intent);
    }else{
        alertDialog.setMessage(result);
        alertDialog.show();
    }*/

    if(result.equals("username or password is incorrect")) {
        Toast.makeText( context, result, Toast.LENGTH_LONG ).show();
    }
    if(result.equals("AD")){
        Intent admin=new Intent(context,Admin.class);
        context.startActivity(admin);
    }
    if(result.equals("EN")){
        Intent engineer=new Intent(context,Engineer.class);
        context.startActivity(engineer);
    }
    if(result.equals("SC")){
        Intent service=new Intent(context,Service.class);
        context.startActivity(service);
    }
    if(result.equals("SLP")){
        Intent Sperson=new Intent(context,SalesPerson.class);
        context.startActivity(Sperson);
    }
    if(result.equals("SLSC")){
        Intent Sservice=new Intent(context,SalesService.class);
        context.startActivity(Sservice);
    }
    if(result.equals("SLSU")){
        Intent SalesSuper=new Intent(context,SalesSupervisior.class);
        context.startActivity(SalesSuper);
    }
    if(result.equals("SU")){
        Intent Supervisior=new Intent(context,Supervisior.class);
        context.startActivity(Supervisior);
    }
  }
}

现在我想比较使用$ row [“emp_role”],我已经在做了但是在比较之后我想通过但是现在我想在下一个意图传递$ row [“emp_id”]。

if(result.equals("AD")){
    Intent admin=new Intent(context,Admin.class);
    context.startActivity(admin);

感谢所有回答我的问题的人。

php android backgroundworker
2个回答
0
投票

在您的PHP代码中,您可以写:

if ( $row["emp_role"] == "ADMIN")
  echo $row["emp_id"] + "-ADMIN";
else
  echo $row["emp_id"];

在您的Android代码中,您可以编写:

if(result.endsWith("-ADMIN")){
    Intent admin=new Intent(context,Admin.class);
    context.startActivity(admin);
}
else if(result.equals("AD")){
    Intent notAdmin=new Intent(context,NotAdmin.class);
    context.startActivity(notAdmin);
}

0
投票

在你的背景工作者中你有这个:

if(result.equals("AD")){
    Intent admin=new Intent(context,Admin.class);
    admin.putExtra("username", username);
    context.startActivity(admin);

然后在您的Admin活动中,您需要在onCreate()方法中执行此操作:

        String userName;
        if(getIntent().getExtras() != null){
            userName = (String) getIntent().getExtras().get("username");
        }

我希望它对你有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.