duanguilin2007 2019-06-26 09:38
浏览 197

无法将移动设备连接到本地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

  • 写回答

3条回答 默认 最新

  • dsj8000 2019-06-26 09:43
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败