duanqia9034 2018-09-05 09:05
浏览 99
已采纳

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

I am new to programming in Android Studio.

I have connected my app to database using php, ms sql.

My php code :

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";
}

This is my backgroundwroker code

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);
    }
  }
}

Now i want to compare using $row["emp_role"], which i am already doing but after comparing i want to pass but now i want to pass $row["emp_id"] in next intent that is here.

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

Thank you to all those who answer my query.

  • 写回答

2条回答 默认 最新

  • drpmazn9021 2018-09-05 09:22
    关注

    In your php code you can write:

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

    In your android code you can write:

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器