1

I'm using xammp to test my backend scripting and I just need to get some JSON with retrofit. I have tried following tutorials online but to no avail. I get the error code

    java.net.ConnectException: Failed to connect to /127.0.0.1:80

Whether I'm using localhost as the baseURL or the Ip address of localhost/xammp server or Whether I explicitly use the port or not, I get this error message or something like stated above. edit: upon closer inspection of the error log it also says:

     Caused by: android.system.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)

and

     Caused by: java.net.ConnectException: failed to connect to /127.0.0.1 (port 80) from /127.0.0.1 (port 58156) after 10000ms: isConnected failed: ECONNREFUSED (Connection refused)

could this have anything to do with it?

my full jsontest.php

{
    "resoponse": {
        "check": "ahhhhhhhh"
    },
    "Mlist": [{
            "text1": "text1a",
            "text2": "text2a",
            "text3": "text3a"
        },
        {
            "text1": "text1b",
            "text2": "text2b",
            "text3": "text3b"
        },
        {
            "text1": "text1c",
            "text2": "text2c",
            "text3": "text3c"
        }
    ]
}

simple api.kt

interface SimpleApi {
    @GET("jsontest.php")
    suspend fun phonehome(): Phone
}

main fragment.kt

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_main, container, false)
        mUserViewModel = ViewModelProvider(this).get(ViewModel::class.java)

            //other code

            TestGet()

        return view
    }
val BASE_URL = "http://127.0.0.1/"
    private fun TestGet() {

        val api = Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(SimpleApi::class.java)

        GlobalScope.launch(Dispatchers.IO) {
          // doing stuff with response
        }
}

if you could point me in the right direction I would be very thankful

thank you.

0

2 Answers 2

2

Is your server running on the same device as your Android app? Only then you can use localhost/127.0.0.1.

6
  • yes the emulator is running on the device as my xammp server Commented Jan 12, 2021 at 19:59
  • Yes? Please reread my comment. I asked where the server was running. Not the emulator. Then localhost/127.0.01 for your app is the emulator. But your server does not run on the emulator. Hence no connection in this way.
    – blackapps
    Commented Jan 12, 2021 at 20:01
  • really? ok, how do I get my xammp stack working in conjunction with my android emulator? Commented Jan 12, 2021 at 20:03
  • Use ip 10.0.2.2 for that.
    – blackapps
    Commented Jan 12, 2021 at 20:06
  • hot damn it worked! thank you, brother. do you mind making the solution an answer so I can give it the green check mark? Commented Jan 12, 2021 at 20:09
0

Is your server on port 80? By default its on 8000 or 8080, if that is the case your URL (BASE_URL) would look like this: http://127.0.0.1:xxxx/, xxxx being your port number, also check your IP address and make sure you can access the URL from outside the machine.

5
  • so upon closer inspection of the error logs it also says: ` Caused by: java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 80) from /127.0.0.1 (port 58160) after 10000ms: isConnected failed: ECONNREFUSED (Connection refused) ` Commented Jan 12, 2021 at 19:31
  • any yes I can connect to localhost with just 127.0.0.1:80 in my web browser. Commented Jan 12, 2021 at 19:36
  • Webbrowser of Android device?
    – blackapps
    Commented Jan 12, 2021 at 19:53
  • it works in my computers web browser but not the web browser of the android emulator Commented Jan 12, 2021 at 20:00
  • Indeed. As localhost/127.0.0.1 is not usable then. And now you know why.
    – blackapps
    Commented Jan 12, 2021 at 20:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.