簡體   English   中英

似乎無法登錄或連接到MySQL數據庫

[英]Can't seem to login or connect to MySQL database

我正在使用這個登錄Android應用程序的實際示例進行操作,用戶可以輸入我已經在數據庫中分配的用戶名和密碼,然后收到標題為“登錄狀態”的消息和消息:“登錄成功”如果他們正確輸入了憑據,否則輸入“登錄失敗”。

但是,每次我輸入正確的用戶名和密碼時,都會收到消息標題“ Login Status”(登錄狀態),而不是消息,以及是否成功。 以下是必要的代碼。

主要活動.java:

package com.godiegodie.dbtrial;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    EditText usernameEt;
    EditText passwordEt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        usernameEt = (EditText)findViewById(R.id.etUsername);
        passwordEt = (EditText)findViewById(R.id.etPassword);
    }

    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(username,password,type);

    }
}

主要活動xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.godiegodie.dbtrial.MainActivity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/etUsername"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:hint="username" />

    <Button
        android:text="Login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="81dp"
        android:id="@+id/btnLogin"
        android:layout_below="@+id/etPassword"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:onClick="onLogin"/>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:layout_marginTop="31dp"
        android:id="@+id/etPassword"
        android:hint="password"
        android:layout_below="@+id/etUsername"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

BackgroundWorker.java類:

package com.godiegodie.dbtrial;

import android.app.AlertDialog;
import android.content.Context;
import android.os.AsyncTask;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

/**
 * Created by Alcatraz on 1/15/2017.
 */

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.1.41/login.php";
        if(type.equals("login")){
            try {
                String user_name = 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(user_name,"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) {
        alertDialog.setMessage(result);
        alertDialog.show();

    }

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

login.php中:

<?php 
require "conn.php";
$username = $_POST['user_name'];
$userpass = $_POST['password'];
$mysql_qry = "select * from employee_data where username like '$username' and password like '$userpass'; ";
$result = mysqli_query($conn ,$mysql_qry);

if(mysqli_num_rows($result) > 0){
    echo "Login successfull";
}
else{
    echo "Login failed";
}
?>

conn.php:

<?php 

    $db_name = "employee101";
    $mysql_username = "root";
    $mysql_password = "";
    $server_name = "localhost";

    $conn = mysqli_connect($server_name,$mysql_username, $mysql_password,$db_name);

    ?>

任何幫助將不勝感激。

看來你不傳遞正確的值到您.execute(String,String)方法,這意味着您AsyncTask<String,Void,String>有3個參數StringdoInBackground和另一個String為結果。

創建一個String數組,該數組將保存您在doInBackground中使用的所有Login值:

更正的代碼:

public void onLogin(View view){
  String username = usernameEt.getText().toString();
    String password = passwordEt.getText().toString();
    String[] params = {"Login","username","password"};

    BackgroundWorker backgroundWorker = new BackgroundWorker(this);
    backgroundWorker.execute(params,"");

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM