dongxiangxie8181 2019-03-17 22:36
浏览 45
已采纳

我的PHP代码连接数据库的android

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

Android manifest i added

 <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

  • 写回答

1条回答 默认 最新

  • douxiajia6309 2019-03-17 23:09
    关注

    this line

    if ($Email == $row ['Email'] and $Password == $dbPassword) {
    

    is wrong/the problem. You already password_verified, which returns a boolan, so it should be.

    if ($Email == $row ['Email'] and $Password) {
    

    And you don't need to re-check if $Email == $row ['Email'], because you queried for it already.

    So you can reduce your code to:

    if (mysqli_num_rows($res)>0) {
       $dbPassword = $row['Password'];
    
       if (PASSWORD_VERIFY($Password, $dbPassword)) {
         $id = $row['id'];
         $_SESSION['id'] = $id;
         exit();
    
       } else {
       //...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器