Java 如何将登录活动定向到不同的用户配置文件

Java 如何将登录活动定向到不同的用户配置文件,java,android,login,Java,Android,Login,我正在做一个android应用程序,登录后,我有两种不同类型的用户配置文件在我的情况下,一个简单的客户端和一个具有高级配置的药剂师,我一直在互联网上寻找如何根据在我的数据库中注册的用户帐户指导我的登录活动,但没有帮助,我正在使用android studio。这是我的loginactivity.java package com.example.yh.log; import android.app.Activity; import android.support.v7.app.AppCompatA

我正在做一个android应用程序,登录后,我有两种不同类型的用户配置文件在我的情况下,一个简单的客户端和一个具有高级配置的药剂师,我一直在互联网上寻找如何根据在我的数据库中注册的用户帐户指导我的登录活动,但没有帮助,我正在使用android studio。这是我的loginactivity.java

package com.example.yh.log;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MainActivity extends Activity {
    private EditText TextUserName;
    private EditText TextPassword;
    public static final String USER_NAME = "USERNAME";
    String username;
    String password;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



}
public void onLoginClick(View view){
TextUserName = (EditText) findViewById(R.id.editusername);
TextPassword = (EditText) findViewById(R.id.editpassword);

username=TextUserName.getText().toString();
password=TextPassword.getText().toString();
if(username.isEmpty())
    Toast.makeText(getBaseContext(),"Entrez votre username",Toast.LENGTH_SHORT).show();
else if(password.isEmpty())
    Toast.makeText(getBaseContext(),"Entrez votre mot de passe",Toast.LENGTH_SHORT).show();
else {
    String urlString = "http://192.168.173.1/Search/login.php";
    LoginTask loginTask = new LoginTask();
    loginTask.execute(urlString);
}
}
private class LoginTask extends AsyncTask<String,Void,String>
{
    private Dialog loadingDialog;
  @Override
  protected void onPreExecute() {
  super.onPreExecute();
 loadingDialog = ProgressDialog.show(MainActivity.this, "Please wait", "Loading...");
}
    @Override
    protected String doInBackground(String... params) {
        HttpURLConnection c=null;
        try {
            String urlString=params[0];
            URL url=new URL(urlString);
            c=(HttpURLConnection)url.openConnection();
            c.setRequestMethod("POST");
            c.setConnectTimeout(15000 /* milliseconds */);
            c.setDoInput(true);
            c.setDoOutput(true);
            c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            String s = "username="+username+"&password=" + password;
        c.setFixedLengthStreamingMode(s.getBytes().length);
        PrintWriter out = new PrintWriter(c.getOutputStream());
        out.print(s);
        out.close();

        c.connect();
        int mStatusCode = c.getResponseCode();
        String result="";
        switch (mStatusCode) {
            case 200:
                BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
                StringBuilder sb = new StringBuilder();
                String line;
                while ((line = br.readLine()) != null) {
                    sb.append(line).append("\n");
                }
                br.close();
                result =  sb.toString();

        }
        return result;
    } catch (Exception ex) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
        return "Error connecting to server";
    } finally {
        if (c != null) {
            try {
                c.disconnect();
            } catch (Exception ex) {
               Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
@Override
protected void onPostExecute(String s) {
        //super.onPostExecute(s);
        String ss = s;
        loadingDialog.dismiss();
        if(ss.equals("successclient\n")) {
            Intent intent = new Intent(MainActivity.this, UserProfile.class);
        }else {
            if (ss.equals("successpharmacien\n")) {
                Intent intent = new Intent(MainActivity.this, PharmacienProfile.class);

            intent.putExtra(USER_NAME, username);
            finish();
            startActivity(intent);
        }else{
               Toast.makeText(getApplicationContext(), ss, Toast.LENGTH_LONG).show();

    }}
}
}
package com.example.yh.log;
导入android.app.Activity;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.app.Dialog;
导入android.app.ProgressDialog;
导入android.content.Intent;
导入android.os.AsyncTask;
导入android.support.v7.app.ActionBarActivity;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
导入android.widget.Toast;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.io.PrintWriter;
导入java.io.UnsupportedEncodingException;
导入java.net.HttpURLConnection;
导入java.net.URL;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.logging.Level;
导入java.util.logging.Logger;
公共类MainActivity扩展了活动{
私有编辑文本文本用户名;
私人编辑文本密码;
公共静态最终字符串USER\u NAME=“USERNAME”;
字符串用户名;
字符串密码;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
公共仅限作废单击(视图){
TextUserName=(EditText)findViewById(R.id.editusername);
TextPassword=(EditText)findViewById(R.id.editpassword);
username=TextUserName.getText().toString();
password=TextPassword.getText().toString();
if(username.isEmpty())
Toast.makeText(getBaseContext(),“Entrez votre用户名”,Toast.LENGTH_SHORT.show();
else if(password.isEmpty())
Toast.makeText(getBaseContext(),“Entrez votre mot de passe”,Toast.LENGTH_SHORT).show();
否则{
字符串URL字符串=”http://192.168.173.1/Search/login.php";
LoginTask LoginTask=新的LoginTask();
loginTask.execute(urlString);
}
}
私有类LoginTask扩展异步任务
{
私有对话框加载对话框;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
loadingDialog=ProgressDialog.show(MainActivity.this,“请稍候”,“正在加载…”);
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
HttpURLConnection=null;
试一试{
字符串urlString=params[0];
URL=新URL(URL字符串);
c=(HttpURLConnection)url.openConnection();
c、 setRequestMethod(“POST”);
c、 setConnectTimeout(15000/*毫秒*/);
c、 setDoInput(true);
c、 设置输出(真);
c、 setRequestProperty(“内容类型”、“应用程序/x-www-form-urlencoded”);
字符串s=“username=“+username+”&password=“+password;
c、 setFixedLengthStreamingMode(s.getBytes().length);
PrintWriter out=新的PrintWriter(c.getOutputStream());
打印出来;
out.close();
c、 connect();
int mStatusCode=c.getResponseCode();
字符串结果=”;
开关(mStatusCode){
案例200:
BufferedReader br=新的BufferedReader(新的InputStreamReader(c.getInputStream());
StringBuilder sb=新的StringBuilder();
弦线;
而((line=br.readLine())!=null){
sb.append(行)。append(“\n”);
}
br.close();
结果=sb.toString();
}
返回结果;
}捕获(例外情况除外){
Logger.getLogger(getClass().getName()).log(Level.SEVERE,null,ex);
返回“连接到服务器时出错”;
}最后{
如果(c!=null){
试一试{
c、 断开连接();
}捕获(例外情况除外){
Logger.getLogger(getClass().getName()).log(Level.SEVERE,null,ex);
}
}
}
}
@凌驾
受保护的void onPostExecute(字符串s){
//super.onPostExecute(s);
字符串ss=s;
loadingDialog.disclose();
if(ss.equals(“successclient\n”)){
意向意向=新意向(MainActivity.this、UserProfile.class);
}否则{
if(ss.equals(“成功药剂师”)){
意向意向=新意向(MainActivity.this,pharmacenProfile.class);
intent.putExtra(用户名、用户名);
完成();
星触觉(意向);
}否则{
Toast.makeText(getApplicationContext(),ss,Toast.LENGTH_LONG.show();
}}
}
}
这是php代码

<?php


 try
{
    // connecting to MySQL
    $bdd = new PDO('mysql:host=localhost;dbname=application;charset=utf8', 'root', '');

}
catch(Exception $e)
{

// In case of an error, we display a message and stop everything
    die('Erreur : '.$e->getMessage());

}
    //Getting values 
    $username = $_POST['username'];
    $password = $_POST['password'];



//contact surgat
// display every input one after another

$listephar = $bdd->query("SELECT * FROM phamacien  WHERE emailpharmacien='$username' AND motpasspharmacien='$password'");

$listeclient = $bdd->query("SELECT * FROM client  WHERE emailclient='$username' AND motpass='$password'");

while ($pharmacien = $listephar->fetch())
{

    //if we got some result 
    if(isset($pharmacien)){
        //displaying success 
        echo "successpharmacien";
        }else{
        //displaying failure


while ($client = $listeclient->fetch())
{
    if (isset($client)){
            echo "successclient";
        }else{
        //displaying failure
        echo "failure";


}
}}}
$listephar->closeCursor();      
$listeclient->closeCursor(); 
?>

成功登录后,从服务器返回用户类型

if(ss=="success") {


    if(usertype=="guest")
    {
    Intent intent = new Intent(MainActivity.this,UserProfile.class);         
    }else{
    Intent intent = new Intent(MainActivity.this, AdminProfile.class);
    }
    intent.putExtra(USER_NAME, username);
    startActivity(intent);
}
else{
       Toast.makeText(getApplicationContext(), "invalide username or password", Toast.LENGTH_LONG).show();

}}

但是我在php代码中做测试,我在“fetch()”中得到了错误,我将编辑codecomments,应该是英语,而不是法语