1

I'm trying to connect my android app with an API that runs on my PC.

I'm testing the app on my actual phone. The base URL is http://localhost:5000/api/.

object RequestManager {
    val interceptor = HttpLoggingInterceptor()
    val client = OkHttpClient.Builder().addInterceptor(interceptor).build()


    init {
        //TODO must be None at live
        interceptor.level = HttpLoggingInterceptor.Level.BODY
    }


    val retrofit = Retrofit.Builder()
        .baseUrl("http://localhost:5000/energy/api/")
        .addConverterFactory(GsonConverterFactory.create())
        .client(client)
        .build()

    val service = retrofit.create(Api::class.java)

}

I tried to go with http://< myIP >:5000/energy/api/ but I get a bad request message.

>   D/OkHttp: <-- 400 Bad Request

Any help?

ZookKep
  • 481
  • 5
  • 13
  • Android has deprecated the use of clear text http traffic. Link(https://stackoverflow.com/questions/45940861/android-8-cleartext-http-traffic-not-permitted) to a related post, make sure you are not a victim of this. If yes, you will end up having an IOException like this - "..Exception: IOException java.io.IOException: Cleartext HTTP traffic to * not permitted" – Som Feb 25 '20 at 00:24
  • @Som Yes that was a problem for me before, but I found that exact post and made it right. So it's not the problem now – ZookKep Feb 25 '20 at 00:26

2 Answers2

2

Try using ngrok. Could be solution to your problem as you insert localhost and port and it gives you a URL to work with.

Littlish
  • 455
  • 2
  • 11
0

If you are using this base URL is http://localhost:5000/api/ or <PC IP> then run your app on emulator not on actual device because all code and api are running in your local server.

Divyanshu
  • 462
  • 6
  • 17