【问题标题】:Why is connecting to the server with retrofit not working?为什么通过改造无法连接到服务器?
【发布时间】:2020-07-03 17:37:00
【问题描述】:

到目前为止,我已经尝试了所有在互联网上找到的方法,但仍然无法连接到服务器。 这是 network_security_config 文件:

    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">127.0.0.1</domain>
        <domain includeSubdomains="true">localhost</domain>
        <domain includeSubdomains="true">http://127.0.0.1:8000/laravel/allamvizsga_backend/public/api/</domain>
    </domain-config>
</network-security-config>

我也将它包含在清单文件中。 之前的错误是“网络安全策略不允许明文通信”,修改后是“无法连接到/127.0.0.1 服务器”。可能是什么问题?

我的api客户端界面:

private static final String BASE_URL = "http://127.0.0.1:8000/laravel/allamvizsga_backend/public/api/";
 public static Retrofit getRetrofitInstance(){
     if (retrofit == null) {
         retrofit = new retrofit2.Retrofit.Builder()
                 .baseUrl(BASE_URL)
                 .addConverterFactory(GsonConverterFactory.create())
                 .build();
     }
     return retrofit;
 }

还有一条路径:

@FormUrlEncoded
    @POST("login")
    Call<Result> loginUser(
            @Field("email") String email,
            @Field("password") String password);

【问题讨论】:

    标签: android laravel api android-studio retrofit


    【解决方案1】:

    127.0.0.1 是指运行代码的设备上的环回接口。我怀疑那里有任何服务器在运行。

    假设您的服务器在运行模拟器的同一台计算机上,请改用 10.0.2.2。

    更多阅读:https://developer.android.com/studio/run/emulator-networking

    【讨论】:

      猜你喜欢
      • 2022-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-13
      • 1970-01-01
      • 1970-01-01
      • 2017-08-31
      • 2011-10-11
      相关资源
      最近更新 更多