Android应用程序与mysql数据库连接

时间:2018-07-26 11:08:44

标签: android mysql xampp

我尝试将我的android应用程序连接到MySQL数据库。数据库使用postman通过php文件检查连接,但仍然无法将android应用程序连接到mysql数据库。我不知道这是我的code.stop运行应用程序的原因。这里我包括了我的束线部分

dbConnect.php`

<?php

    $DB_NAME ="garment";
    $DB_USER ="root";
    $DB_PASSWORD ="";
    $DB_HOST ="localhost";

    //Connecting to Database
    $con = mysqli_connect($DB_HOST,$DB_USER,$DB_PASSWORD,$DB_NAME);
if(!$con)
{
//echo "Connection Error..".mysqli_connect_error();
}
else
{
//echo"<h3>Database Connectrion success...</h3>";
}
?>

`

userLogin.php`

<?php

    require_once('dbConnect.php');

    $username = $_POST["username"];
    $password = $_POST["password"];

    $sql = "select * from login where username like '$username' and password like '$password';";

    $res = mysqli_query($con,$sql);



    if (mysqli_num_rows($res)>0) 
        {
            $row=mysqli_fetch_assoc($result);
            $name=$row["username"];
            Echo "Login success"
        }
    else
        {
            echo "failure";
        }

?>

MainActivity.java

 package com.example.gagana.garment;

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


public class MainActivity extends AppCompatActivity {

    private EditText usernametxt,passwordtxt,factoryname;
    private Button login;
    String username,password;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        usernametxt=(EditText)findViewById(R.id.txtusername);
        passwordtxt=(EditText)findViewById(R.id.txtpassword);
        factoryname=(EditText)findViewById(R.id.factoryname);
        login=(Button)findViewById(R.id.btnlogin);

    }

    public void userLogin(View view)
    {
        username = usernametxt.getText().toString();
        password = passwordtxt.getText().toString();
        String method="login";
        Constants constants=new Constants(this);
        constants.execute(method,username,password);
    }


}

Constants.java

package com.example.gagana.garment;

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.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;

import javax.net.ssl.HttpsURLConnection;

public class Constants extends AsyncTask<String,Void,String> {
    AlertDialog alertDialog;

    Context ctx;
    Constants(Context ctx)
    {
        this.ctx=ctx;
    }
    @Override
    protected void onPreExecute() {
        alertDialog = new AlertDialog.Builder(ctx).create();
        alertDialog.setTitle("Login Information.....");
    }
    private static final String Root_URL = "http://192.168.8.10/android/userLogin.php";

    @Override
    protected String doInBackground(String... strings) {
        String method = strings[0];
        if(method.equals("login"))
        {
            String username=strings[1];
            String password=strings[2];

            try {
                URL url = new URL(Root_URL);
                HttpsURLConnection httpsURLConnection=(HttpsURLConnection)url.openConnection();
                httpsURLConnection.setRequestMethod("POST");
                httpsURLConnection.setDoOutput(true);
                httpsURLConnection.setDoInput(true);
                OutputStream outputStream = httpsURLConnection.getOutputStream();
                BufferedWriter bufferedWriter =new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
                String data= URLEncoder.encode("username","UTF-8")+"="+URLEncoder.encode(username,"UTF-8")+"&"+
                        URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();

                InputStream inputStream=httpsURLConnection.getInputStream();
                BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
                String response="";
                String line="";
                while ((line=bufferedReader.readLine())!=null)
                {
                    response+=line;
                }
                bufferedReader.close();
                inputStream.close();
                httpsURLConnection.disconnect();
                return response;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return null;
    }

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

    @Override
    protected void onPostExecute(String s) {

        alertDialog.setMessage(s);
        alertDialog.show();
    }
}
`

0 个答案:

没有答案