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