Can't connect to localhost webapi from android studio project

Viewed 234

I am trying to connect to my c# web api on localhost. The endpoints work fine on swagger and postman.

But in my android studio application I can't seem to find a way to connect to my api.

I am using retrofit.

My service looks like this:

private const val BASE_URL = "http://10.0.2.2:5000/api/"


private val  retrofit = Retrofit.Builder()
    .addConverterFactory(ScalarsConverterFactory.create())
    .addConverterFactory(GsonConverterFactory.create())
    .baseUrl(BASE_URL)
    .build()



interface ApiService {
    @POST("User/login")
    fun login(@Body body: LoginRequest): Call<String>
}

note: when I use http://10.0.2.2:5000/api/ I get this error:

Response{protocol=http/1.1, code=307, message=Temporary Redirect, url=http://10.0.2.2:5000/api/User/login}

I tried adding redirect to my client but that does not change the response

But when https://10.0.2.2:5001/api/ I get this error:

javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

I know both errors may have nothing to do with eachother.

I want to use HTTPS but can't manage to make that work and even when I use HTTP my call still returns 307.

I tried adding the localhost certificate like: https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-view-certificates-with-the-mmc-snap-in and adding this to trusted root Certification Authorities - Certificates

btw both HTTP and HTTPS works in swagger and postman

1 Answers

Remove https redirection while debugging

app.UseHttpsRedirection();
Related