im trying to connect my android app with the data base im using for the website but somehow it doesnt want to connect
this is my java code im using for the connect
<uses-permission android:name="android.permission.INTERNET"></uses-
permission>
background.java
package com.example.myapplication;
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;
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://10.0.2.2//android/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.setDoInput(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);
}
}
login.java
package com.example.myapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class login extends AppCompatActivity implements View.OnClickListener
{
Button liButton;
EditText liEmail, liPassword;
TextView liSignup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
liEmail = (EditText) findViewById(R.id.liEmail);
liPassword = (EditText) findViewById(R.id.liPassword);
liButton =(Button) findViewById(R.id.liButton);
liSignup= (TextView) findViewById(R.id.liSignup);
liButton.setOnClickListener(this);
liSignup.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.liButton:
String Email = liEmail.getText().toString();
String Password = liPassword.getText().toString();
String type = "login";
BackgroundWorker backgroundworker = new BackgroundWorker(this);
backgroundworker.execute(type, Email, Password);
break;
case R.id.liSignup:
startActivity(new Intent(this, signup.class));
break;
}
}
}
this is my php code
<?php
$conn = mysqli_connect('127.0.0.1','root','','signup');
?>
<?php
include('conn.php');
if(!session_id())
session_start();
if (isset($_POST['Email'])
and isset($_POST['Password'])
and !empty($_POST['Email'])
and !empty($_POST['Password'])){
$Email = $_POST['Email'];
$Password= $_POST['Password'];
$getinfo = "SELECT * FROM users WHERE Email ='$Email' LIMIT 1";
$res = mysqli_query($conn,$getinfo);
$row = mysqli_fetch_assoc($res);
if (mysqli_num_rows($res)>0) {
$dbPassword = $row['Password'];
$Password = PASSWORD_VERIFY($Password, $dbPassword);
if ($Email == $row ['Email'] and $Password == $dbPassword) {
$id = $row['id'];
$_SESSION['id'] = $id;
exit();
} else{
echo 'Wrong Email or Password.';
}
} else{
echo 'Wrong Email or Password.';
}
}
?>
i believe my java code is right but im not sure what to do with my php if there is something wrong
答案 0 :(得分:0)
此行
if ($Email == $row ['Email'] and $Password == $dbPassword) {
是错误的/有问题。您已经password_verified,它返回一个布尔值,所以应该如此。
if ($Email == $row ['Email'] and $Password) {
因为您已经查询过$ Email == $ row ['Email'],所以您无需重新检查。
因此,您可以将代码简化为:
if (mysqli_num_rows($res)>0) {
$dbPassword = $row['Password'];
if (PASSWORD_VERIFY($Password, $dbPassword)) {
$id = $row['id'];
$_SESSION['id'] = $id;
exit();
} else {
//...