0
我正在工作機智這個登錄Android應用程序的實際例子,用戶可以輸入他們的用戶名和密碼,我已經在數據庫中分配,然後收到一條消息標題「登錄狀態」和消息:「登錄成功」,如果他們正確地輸入了他們的憑證,並且「登錄失敗」,否則。似乎無法登錄或連接到MySQL數據庫
但是每次輸入正確的用戶名和密碼時,我都會收到消息標題「登錄狀態」,但不是消息,以及是否成功。以下是必要的代碼。
主要activity.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);
?>
任何幫助將不勝感激。
這可能會引起這麼多的事情......你測試你的應用程序在本地使用您的手機或Android Studio,並在本地主機上連接到您的PHP腳本? –
您是否檢查過您的網絡服務器的訪問日誌,以查看請求是否實際到達它? – Philipp
如果確實如此,請檢查連接是否成功。用error_log()記錄錯誤# – RiggsFolly