我创建了一个应用程序,用户可以通过输入他们的电子邮件和密码来登录,活动的数据库是从php my admin with xampp创建的。数据库名称是用户,表名是详细信息。我通过输入数据库中的电子邮件和密码来测试它,但它没有做出任何反应。我已经包含了login.php,conn.php,login.java,backgroundWorker.java类。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1647a3"
android:orientation="vertical"
android:paddingEnd="2sp"
android:paddingStart="2sp"
tools:context="com.example.xxxxxx.xxxxxxxxxxx.login"
android:weightSum="1">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="7sp"
android:contentDescription="@string/icon"
android:paddingEnd="10sp"
android:paddingStart="10sp"
android:src="@drawable/icon"
tools:ignore="InefficientWeight"
android:layout_weight="0.52" />
<TextView
android:id="@+id/txt_itst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:freezesText="true"
android:paddingEnd="10sp"
android:paddingStart="10sp"
android:fontFamily="sans-serif-smallcaps"
android:text="@string/app_name"
android:textAlignment="center"
android:textColor="#cac6bc"
android:textSize="32sp" />
<TextView
android:id="@+id/txtLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp"
android:paddingBottom="4sp"
android:paddingEnd="16sp"
android:paddingStart="16sp"
android:text="@string/login_nw"
android:textColor="#212121"
android:textSize="16sp" />
<EditText
android:id="@+id/Et_email"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginEnd="5sp"
android:layout_marginStart="5sp"
android:layout_marginTop="5sp"
android:background="@drawable/passord"
android:cursorVisible="false"
android:ems="10"
android:hint="@string/e_mail"
android:inputType="textEmailAddress"
android:paddingEnd="15sp"
android:paddingStart="20sp"
android:textColor="#d9d6d6"
android:textColorHint="#292828" />
<EditText
android:id="@+id/Password"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginEnd="5dp"
android:textColor="#d9d6d6"
android:layout_marginStart="5dp"
android:layout_marginTop="20dp"
android:background="@drawable/passord"
android:cursorVisible="false"
android:ems="10"
android:hint="@string/pass_w"
android:inputType="textPassword"
android:paddingEnd="15dp"
android:paddingStart="20dp"
android:textColorHint="#292828" />
<Button
android:id="@+id/B_login"
android:layout_width="match_parent"
android:layout_height="36dp"
android:layout_marginEnd="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="20dp"
android:background="@drawable/passord"
android:text="@string/lgn"
android:textColor="#ffffff"
android:textSize="20sp" />
<Button
android:id="@+id/B_register"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginEnd="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="20dp"
android:background="@android:color/transparent"
android:text="@string/reg_now"
android:textColor="#ffffff"
android:textSize="20sp" />
</LinearLayout>
package com.example.sayuru.itstimetobeginsinanotherday;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
public class login extends AppCompatActivity {
EditText Etemail, Etpassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Etemail=(EditText)findViewById(R.id.Et_username);
Etpassword=(EditText)findViewById(R.id.Et_pssword);
}
public void onLogin(View view){
String email=Etemail.getText().toString();
String password=Etpassword.getText().toString();
String type="login";
BagroundWorker bagroundWorker=new BagroundWorker(this);
bagroundWorker.execute(type, email, password);
}
}
package com.example.xxxxx.xxxxxxxxxxx;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.AlertDialog;
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;
public class BagroundWorker extends AsyncTask<String, Void, String> {
Context context;
AlertDialog alertDialog;
BagroundWorker(Context ctx){
context=ctx;
}
@Override
protected String doInBackground(String... params) {
String type=params[0];
String login_url="http:// 10.0.2.2/login.php";
if (type.equals("login")){
try {
String email=params[1];
String password=params[2];
URL url=new URL(login_url);
HttpURLConnection httpURLConnection=
(HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoOutput(true);
OutputStream outputStream=httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter=new BufferedWriter(new
OutputStreamWriter(outputStream, "UTF-8"));
String post_data= URLEncoder.encode("email","UTF-
8")+"="+URLEncoder.encode(email,"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);
}
}
<?php
$db_name = "user";
$mysql_username = "root";
$mysql_password = "";
$server_name = "localhost";
$conn = mysqli_connect($server_name, $mysql_username,
$mysql_password,$db_name);
?>
<?php
require "conn.php";
$user_email = $_POST["email"];
$user_pass = $_POST["password"];
$mysql_qry = "select * from details where email like '$user_email' and
password
like '$user_pass';";
$result = mysqli_query($conn ,$mysql_qry);
if(mysqli_num_rows($result) > 0) {
echo "login success !!!!! Welcome user";
}
else {
echo "login not success";
}
?>
答案 0 :(得分:0)
首先,您必须在 BackgroundWorker.java 类中启用httpURLConnection上的输入(可能是拼写错误):
protected String doInBackground(String... params{
...
httpURLConnection.setDoInput(true);
...
}
始终,尝试以Json格式处理响应并为登录用户存储身份验证密钥。因为我认为这只是尝试从PHP获得原始结果,所以我没有描述更多。
PHP推荐:
更新#1:
<强> login.java 强>
package com.example.sayuru.itstimetobeginsinanotherday;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
public class login extends AppCompatActivity {
EditText Etemail, Etpassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Etemail=(EditText)findViewById(R.id.Et_username);
Etpassword=(EditText)findViewById(R.id.Et_pssword);
}
public void onLogin(View view){
String email=Etemail.getText().toString();
String password=Etpassword.getText().toString();
String type="login";
BagroundWorker bagroundWorker=new BagroundWorker(type, email, password);
bagroundWorker.execute((Void) null);
}
}
<强> BackgroundWorker.java 强>
package com.example.xxxxx.xxxxxxxxxxx;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.AlertDialog;
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;
public class BagroundWorker extends AsyncTask<Void, Void, String> {
AlertDialog alertDialog;
String mType;
String mEmail;
String mPassword;
BagroundWorker(String type, String email, String password){
mType = type;
mEmail = email;
mPassword = password;
}
@Override
protected String doInBackground(Void... params) {
String login_url="http:// 10.0.2.2/login.php";
String result = "";
if (mType.equals("login")) {
try {
URL url=new URL(login_url);
HttpURLConnection httpURLConnection=
(HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
String data = "email=" + URLEncoder.encode(mEmail, "UTF-8") +
"password=" + URLEncoder.encode(mPassword, "UTF-8");
OutputStreamWriter writer = new
OutputStreamWriter(httpURLConnection.getOutputStream());
writer.write(data);
writer.flush();
writer.close();
int status = httpURLConnection.getResponseCode();
if (status == HttpURLConnection.HTTP_OK) {
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
while ((line = br.readLine()) != null) {
result += line;
}
br.close();
}
} catch (Exception e) {
result = false;
} finally {
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
}
return result;
}
return null;
}
@Override
protected void onPreExecute() {
alertDialog=new AlertDialog.Builder(getApplicationContext()).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);
}
}
conn.php 很好,但首选使用PDO。
<强>的login.php 强>
<?php
require "conn.php";
$user_email = $_POST["email"];
$user_pass = $_POST["password"];
$mysql_qry = "select * from details where email like '$user_email' and
password like '$user_pass'";
$result = mysqli_query($conn, $mysql_qry);
if(mysqli_num_rows($result) > 0) {
echo "login success !!!!! Welcome user";
} else {
echo "login not success";
}
此login.php将作为示例使用,但绝不会在生产服务器上使用此类不安全的代码, 这是非常危险的 。