Skip to content
Advertisement

java.net.ConnectException: Failed to connect to /localhost:8080

So, I’m trying to send a POST request from my android phone to my server (laptop) both on the same network. I am getting the error as shown above. I thought this could be something about the firewall thing, but I disabled it. I open the firefox console and see this error The script from “http://192.168.56.1:8080/upload_db.php” was loaded even though its MIME type (“application/x-httpd-php”) is not a valid JavaScript MIME type..

For some more background information, I am uploading (well trying) a .db file from my phone to the server. So here’s my code that I am using to upload this file.

JavaScript

This is the backend code that I’m using is as follows:

JavaScript

For the server setup, I am using the npm http-server. Is this because my server is not listening on port 8080 or is it on the android app end? It’d be great if anyone can point me in the right direction. This seemingly simple task is taking up way too much time. As far as I understand, the server does not require any authentication to accept post requests as I start the server using http-server .. The server opens just fine on the phone browser and I can see the index.html that was written. So, I know that server is operational. However, I have no idea where I am going wrong in the mobile app part.

Advertisement

Answer

The JS (NPM) HTTP Server doesn’t execute PHP scripts. You’d have to setup the infamous WAMP stack so that the PHP script is executed when you call it via URL.

Setting Up Apache/MySQL/PHP (AMP) on Linux (LAMP), Windows (WAMP) and Mac OS (MAMP)

On a side note, you can also use ADB Reverse to forward a port from your device to your laptop.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement