为什么通过改装无法连接到服务器?

Why is connecting to the server with retrofit not working?

到目前为止,我已经尝试了所有在互联网上找到的方法,但仍然无法连接到服务器。 这是 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>

我也将其包含在清单文件中。 之前的错误是"cleartext communication to not permitted by network security policy",修改后是"Cannot connect to the /127.0.0.1 server"。可能是什么问题?

我的 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);

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

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

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