Reputation:
I have Wampp PHP server on my local computer (127.0.0.1:80). I am making a android login app that is connecting to mysql server. from browser i have successful connection with server but with android studio is somethings wrong:
W/System.err: java.net.ConnectException: Failed to connect to /127.0.0.1:80
at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:143)
at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:112)
at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:184)
at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:127)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:258)
at org.schools.loginregister.LoginActivity$AsyncLogin.doInBackground(LoginActivity.java:103)
at org.schools.loginregister.LoginActivity$AsyncLogin.doInBackground(LoginActivity.java:59)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
D/EGL_emulation: eglMakeCurrent: 0xcdb6f720: ver 3 0 (tinfo 0xe5efebf0)
D/EGL_emulation: eglMakeCurrent: 0xcdb6f720: ver 3 0 (tinfo 0xe5efebf0)
D/EGL_emulation: eglMakeCurrent: 0xcdb6f720: ver 3 0 (tinfo 0xe5efebf0)
Upvotes: 0
Views: 7040
Reputation: 21
You have to create a php file in www folder where your wamp server situated. In the php file you have the code for connect with app and wampserver. The code look like this
my_data.php----
<?php
$HostName = 'localhost:3308'; //MySQL port number is 3308
$HostUser = 'root';
$HostPass = '';
$DatabaseName = 'my_data';
$con = mysqli_connect($HostName,
$HostUser,$HostPass,$DatabaseName);
// add your code.
mysqli_close($con);
?>
In the app you have to connect like below code
String HTTP_URL = "http://your pc ip address/my_data.php";
//your pc ip address like 190.158.43.187
You can also see my post https://www.puskarcoding.com/project/how-to-build-mcq-app-connect-to-wampserver-by-android-studio/
Upvotes: 0
Reputation: 10270
The Android Emulator is, for all intents and purposes, its own device much like a physical phone or tablet. As a result, the emulator has its own internal web server running to provide internal services. localhost
on the emulator does not point to the same web server as localhost
on your host machine.
In order to access a WAMP server that is running on your host machine, you will need to open up port 80 on your host machine and use your host machine's IP address to connect. The emulator uses Network Address Translation to access your network via its host, so it should be able to access any other network device that your host can access, including the host itself.
Upvotes: 2