无法将移动设备连接到本地MySQL数据库,但可以使用模拟器工作

无法将移动设备连接到本地MySQL数据库,但可以使用模拟器工作

问题描述:

I can't connect my mobile device to my local MySQL database. I'm using Wamp server 64 bits. However it perfectly works on the emulator device. I just followed this tutorial : https://www.youtube.com/watch?v=4e8be8xseqE.

When I run the app on the emulator, the adress 10.0.2.2 works. When I run the app on my own device, I change the adress to 127.0.0.1 or my IPv4. I even tried to connect my mobile to my computer wifi's hotspot, and use the specific IPv4 but that didn't work.

Also I installed Android Terminal Emulator and pinged my compter's IP but that didn't work... That's really strange because my mobile is well connected to the same wifi network than my computer.

Android code to connect to the local MySQL database :

@Override
    protected String doInBackground(String... params) {
        String type = params[0];
        String login_url = "http://192.168.1.34/login.php";
            if(type.equals("login")) {
            try {
                String username = 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("username", "UTF-8") + "=" +
                        URLEncoder.encode(username, "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;
    }

PHP scripts : connect.php

<?php

$db_name = "employee101";
$mysql_username = "root";
$mysql_password = "";
$server_name = "localhost";
$conn = mysqli_connect($server_name, $mysql_username, $mysql_password, $db_name);

?>

login.php

<?php

require "conn.php";

$user_name = $_POST["username"];
$user_name = $_POST["password"];
$user_pass = "123";
$mysql_qry = "select * from employee_data where username like '$user_name' and password like '$user_pass';";

$result = mysqli_query($conn, $mysql_qry);

if(mysqli_num_rows($result) > 0)
{
    echo "login success";
}
else
{
    echo "login not success";
}

?>

So, on the emulator that displays a Toast with the message "login success". On my own device, there is no message. I don't know what's wrong.

Do you have any idea ? Thanks

you need to connect both your system and device in which you testing in one wifi(network) your emulator is in your system so it is working.

can you try using json response

header('Content-Type: application/json');

if(mysqli_num_rows($result) > 0)
{
    $data['status'] = 200;
    $data['message'] = "login success";
    echo json_encode($data);
}
else
{
    $data['status'] = 200;
    $data['message'] = "login not success";
    echo json_encode($data);
}

So, I fixed the problem I couldn't ping my PC from my phone. That was a firewall restriction. I simply added an exception by using this command :

netsh advfirewall firewall add rule name="ICMP Allow incoming V4 echo request" protocol=icmpv4:8,any dir=in action=allow

However, I keep not connecting my phone to my MySQL local database. My phone is connected to my PC's wifi hotspot. His IP is, at the moment, 192.168.137.204.

In my code, I'm using this url : "http://192.168.137.1/login.php" because 192.168.137.1 is my PC's IP (in the local network). But it's still not working...