java - 为什么还是无法进入下一页?

标签 java mysql android-studio connection android-alertdialog

我的问题:

1. No error on it.

2. Cannot go to next page but I already put if else statement for login at onPostExecute.

3. It is true my if else statement?

下面是证明我的按钮可点击但不能转到下一页:

enter image description here

下面是我的 onPostExecute 代码片段:

enter image description here

下面是:Background.java,用于连接mysql数据库。

public class Background extends AsyncTask<String,Void,String> {

Context context;
AlertDialog alertDialog;

Background(Context ctx) {
    context = ctx;
}

@Override
protected String doInBackground(String... params)
{
    String type = params[0];
    String login_url = "http://172.20.10.4/LoginLab3.php";
    String reg_url = "http://172.20.10.4/RegisterLab3.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("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();
        }
    }
    else if(type.equals("register"))
    {
        try {
            String name = params[1];
            String surname = params[2];
            String age = params[3];
            String username = params[4];
            String password = params[5];
            URL url = new URL(reg_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("name","UTF-8")+"="+URLEncoder.encode(name,"UTF-8")+"&"
                    +URLEncoder.encode("surname","UTF-8")+"="+URLEncoder.encode(surname,"UTF-8")+"&"
                    +URLEncoder.encode("age","UTF-8")+"="+URLEncoder.encode(age,"UTF-8")+"&"
                    +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(final String result)
{
    final AlertDialog.Builder dialog = new AlertDialog.Builder(context);
    dialog.setTitle("Login Status");
    dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick(DialogInterface dialogInterface, int i)
        {
            Boolean login = (context.equals("login"));
            if(login==true)
            {
                Intent in = new Intent(context, Welcome.class);
                context.startActivity(in);
                ((Activity)context).finish();
            }
            else
            {
                dialog.setMessage("Wrong username and password");
            }
        }
    });
    dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.cancel();
        }
    });
    dialog.create().show();
}

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

下面是:Login.java

public class Login extends AppCompatActivity
{
EditText username, password;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    username = findViewById(R.id.etUsername);
    password = findViewById(R.id.etPassword);
}

public void OnLog(View view)
{
    String Username = username.getText().toString();
    String Password = password.getText().toString();
    String type = "login";

    if(Username.equals("") || Password.equals(""))
    {
        Toast.makeText(getApplicationContext(), "Username and Password are required!", Toast.LENGTH_LONG).show();
    }
    else {

        Background bg = new Background(this);
        bg.execute(type, Username, Password);
    }
}

public void OnReg(View view) {
    startActivity(new Intent(getApplicationContext(), Register.class));
}
}

最佳答案

if 语句中的条件永远不会为真。

Context 类型的

context 永远不能等于 login 字符串。所以 if 子句永远不会运行。

黄色突出显示的检查可能正在提示这一点。

我认为登录结果将在作为参数传入的字符串result中。您可能必须解析结果并查看登录是否成功。

关于java - 为什么还是无法进入下一页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58102781/

相关文章:

java - 在 textView 内显示可点击的链接

php - 获取每条记录中所有行的总和

php - MySQL:多行结果 VS 单行结果

java - 如何在android中将textview设置为方法输出?

Android Studio - 应用在 AddThis.jar 上崩溃 - "Sources Not Found"消息

java - 使用Gradle添加Google Identity Toolkit时出错

java - 使用 Java Conf 而不是 xml 配置 Spring Wicket

java - 从java运行grep命令后获取错误

java - 如何在maven中管理多应用程序项目?

mysql - SQL 检查一个表中的行是否也存在于另一个表中的最有效方法