-1

I'm developing an android app in android studio and I have a mysql database on my localhost. I'm using XAMPP so I'm using MariaDB and Apache.

When i run the emulator everything works fine but when I use the app in my physical device doesn't work.

I have opened ports 80 and 3306 and give permissions in app manifest:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Java in android activity (I'm using volley for http requests):

public static void Register(String email,String password,Context context){
    StringRequest request = new StringRequest(Request.Method.POST, "http://xx.xx.x.x/android/register.php",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    User.setEmail(email);
                    User.setPassword(password);
                    Toast.makeText(context, "Registrado con éxito", Toast.LENGTH_SHORT).show();
                    context.startActivity(new Intent(context, MainActivity.class));


                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(context, "Ha ocurrido un error",Toast.LENGTH_SHORT).show();
                }
            }
    ){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String,String> params = new HashMap<>();
            params.put("email",email);
            params.put("password",password);
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
            String date = sdf.format(new Date());
            params.put("date",date);

            return params;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(context);
    requestQueue.add(request);
}

xx.xx.x.x is my public ip.

register.php:

<?php

    if ($_SERVER ['REQUEST_METHOD'] == 'POST'){

    require_once("db.php");

    $email = $_POST['email'];
    $password = $_POST['password'];
    $date = $_POST['date'];

    $query = "INSERT INTO user (email, password, date) VALUES 
    ('$email','$password','$date')";

    $result = $mysql->query($query);

    if ($result == TRUE){
        echo "user created";
    }
    else{
        echo "user not created";
    }
    $mysql->close();
  }

db.php: The user rafa in my database has all permissions and can connect by any address. (I have tried to change "localhost" to my public ip but it doesn't work either)

<?php


$mysql = new mysqli(
"localhost",
"rafa",
"1234",
"eduk"
);

if ( $mysql->connect_error){
  die( "failed to connect" . $mysql->connect_error);
}

And the error I am getting is : Ljava.lang.StackTraceElement;

I readed about the problem and people say that I need a hosting and everything will works fine but why I need it?

The emulator is supposed to act like another device but no physical, it's the only difference. I can access to my apache server with the pyshical device, the problem must be on the connection to database or something. Maybe I need to use other library?

  • 2
    Does the PC running apache and the database have a public IP, or is the PC connected to a router via a private IP, with the router acting as a NAT-Gateway? If the latter is the case: Have you configured the router to forward the ports `80` and `3306` to the PC running apache and the database? Have you tried to call the php-file in the browser? If so: does it work? – Turing85 Sep 19 '21 at 12:25
  • 1
    Your local network is not reachable from the internet - your router does prevent that (unless you configure it with port-forwarding). But BE WARNED: This exposes your local machine to the internet (where hackers are waiting...) – Honk der Hase Sep 19 '21 at 12:26
  • If you are using a physical device, you are not anymore in the localhost, correct? You are mentioning the public IP of your computer but I don't see it in your code. – 123pierre Sep 19 '21 at 12:28
  • My router is configured (port forwarding), if not, it will not work in the emulator – Rafael Diaz Sep 19 '21 at 12:29
  • I have oculted my public ip but in code is correct, in the code sample appears like xx.x.xx.x – Rafael Diaz Sep 19 '21 at 12:30
  • If i call the php-file using the browser works correctly – Rafael Diaz Sep 19 '21 at 12:32
  • Could you post the first two numbers of the public IP? – Turing85 Sep 19 '21 at 12:33
  • Yes, 89.xx.x.x. And the port forwarding its configured. – Rafael Diaz Sep 19 '21 at 12:35
  • 1
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Sep 19 '21 at 12:45
  • 1
    **Never store passwords in clear text or using MD5/SHA1!** Only store password hashes created using PHP's [`password_hash()`](https://php.net/manual/en/function.password-hash.php), which you can then verify using [`password_verify()`](https://php.net/manual/en/function.password-verify.php). Take a look at this post: [How to use password_hash](https://stackoverflow.com/q/30279321/1839439) and learn more about [bcrypt & password hashing in PHP](https://stackoverflow.com/a/6337021/1839439) – Dharman Sep 19 '21 at 12:45
  • try to use your private ip and if it work then the issue with your router configuration –  Sep 19 '21 at 12:45
  • I know I haven't done anything about data security, I'm just trying to get it to work, it's a test database, but thank you so much for the information. – Rafael Diaz Sep 19 '21 at 12:50
  • I have tried to do it with private ip but it still does not work :( – Rafael Diaz Sep 19 '21 at 13:03
  • Maybe try to access your database with another computer to make sure your database is indeed accessible over the public IP... Just an idea ;-) – 123pierre Sep 19 '21 at 13:07
  • Yes it's works if I access to php files from another computer or if I access with android physical devices but from browser not by my app. The problem is in the app definitely... – Rafael Diaz Sep 19 '21 at 13:13
  • It's very difficult to debug a crash without a stack trace. See [Unfortunately MyApp has stopped. How can I solve this?](/q/23353173) for Android-specific advice, and [What is a stack trace, and how can I use it to debug my application errors?](/q/3988788) for advice on what to do once you have the stack trace. If you still need help, edit your question to include the **complete stack trace**, as well as **which line of your code** the stack trace points to. – Ryan M Sep 20 '21 at 19:54

1 Answers1

0

I have solved adding this on manifest...

    android:usesCleartextTraffic="true"
  • Hi, I'm facing similar issue but it is still not working even this text is already in my manifest file. Can you check and help if possible? https://stackoverflow.com/questions/74395301/cannot-connect-api-at-locahost-from-android-mobile – Taimoor Nov 11 '22 at 14:47