【问题标题】:Can't connect to localhost webapi from android studio project无法从 android studio 项目连接到 localhost webapi
【发布时间】:2021-03-25 12:15:43
【问题描述】:

我正在尝试在 localhost 上连接到我的 c# web api。 端点在 swagger 和 postman 上运行良好。

但在我的 android studio 应用程序中,我似乎找不到连接到我的 api 的方法。

我正在使用改造。

我的服务如下所示:

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>
}

注意:当我使用 http://10.0.2.2:5000/api/ 时出现此错误:

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

我尝试将重定向添加到我的客户端,但这不会改变响应

但是当 https://10.0.2.2:5001/api/ 我得到这个错误:

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

我知道这两个错误可能彼此无关。

我想使用 HTTPS,但无法让它工作,即使我使用 HTTP,我的调用仍然返回 307。

我尝试添加 localhost 证书,例如:https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-view-certificates-with-the-mmc-snap-in 并将其添加到受信任的根证书颁发机构 - 证书

顺便说一句,HTTP 和 HTTPS 都可以在 swagger 和 postman 中使用

【问题讨论】:

  • 您找到解决方案了吗?我有同样的问题

标签: c# android-studio kotlin ssl https


【解决方案1】:

在调试时移除 https 重定向

app.UseHttpsRedirection();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-21
    • 2019-02-01
    • 2012-01-08
    • 2013-10-30
    • 2019-02-02
    • 1970-01-01
    相关资源
    最近更新 更多