1

first of all I'd like to say that I've been looking for answers in a lot of places, but I just can't seem to find any that work for me. At the end I'll list all the options I've tried already. I'm new to Android and this is driving me nuts.

What I'm trying to do is submit a simple GET request to a local Laravel database running on my Mac. I'm trying to call this request from an Android app on an emulator. My Laravel server is running on 127.0.0.1:8000 and it is turned on. In the .env file it states that DB_PORT=3306, but when changing the port in the request it doesn't matter.

What I'm doing in Android:

String url = "http://127.0.0.1:8000/api/recipes"; // More on the IP at the end
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d("heeftGewerkt", response.substring(30));
            errorMessage = "Succesful register";
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("heeftNietGewerkt", error.getMessage());
            errorMessage = "Unsuccesful register";
        }
    });

My androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.a1114049_1090780_iiatimd_app">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:usesCleartextTraffic="true"
    android:theme="@style/Theme.1114049_1090780_iiatimd_app">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
</manifest>

I understand that when sending a request to 127.0.0.1 it might look for a server on the android device, which it obviously can't find. So I've tried to replace 127.0.0.1:8000 with localhost:8000, local-ip:8000 and public-ip:8000. None of these seem to work.

When I make a request to the Laravel server using Postman (http://127.0.0.1:8000/api/recipes) it works and I get all the recipes in my database. Can anyone clear this up for me?

EDIT: The error I get is 'Failed to connect to /127.0.0.1:8000'. Looking at this it might be because I'm not passing in any DB credentials anywhere, but I'm also not sure if I should.

G Buis
  • 303
  • 4
  • 17

1 Answers1

1

Use 10.0.2.2 to access your actual machine instead of localhost/127.0.0.1

When inside the emulator local host is referring to the emulator itself. However the emulator supports forwarding to the host machine on ip address 10.0.2.2

So you db server address with be

10.0.2.2:8000
DevWithZachary
  • 3,545
  • 11
  • 49
  • 101