在 Android 中的改装 GET 调用中传递参数的正确方法是什么?

What is the right way to pass the parameter in the retrofit GET call in Android?

我想在 android 应用程序中从 AWS 服务器获取数据,以及我为此使用改造的方式。这部分已经解决了。

我想用方法一获取数据,但方法无效。

请考虑基础URL没有问题<DOMAIN/>

方法一:

@GET("/release-1a/vendor/getCustomerProfile")
@Headers("Accept-type: application/json")
fun getCustomerProfile(
        @Query("appId") appId: String?,
        @Query("clientId") clientId: String?,
        @Query("clientPhone") clientPhone: String?
): Observable<GetCustomerProfileResponse?>?

网络请求

CustomerApi customerApi = RetrofitBuilder.getInstance(NEW_CUSTOMER_PROFILE_URL).create(CustomerApi.class);
    Observable<GetCustomerProfileResponse> observable =
            customerApi.getCustomerProfile("4", "5", "%2B919829732808");

使用此方法时,我收到错误代码 400,这意味着请求错误。 但是当我使用方法二时,我得到了想要的结果。

方法二:

@GET("/release-1a/vendor/getCustomerProfile?appId=4&clientId=5&clientPhone=%2B919829732808")
@Headers("Accept-type: application/json")
fun getCustomerProfile(): Observable<GetCustomerProfileResponse?>?

网络请求

CustomerApi customerApi = RetrofitBuilder.getInstance(NEW_CUSTOMER_PROFILE_URL).create(CustomerApi.class);
    Observable<GetCustomerProfileResponse> observable =
            customerApi.getCustomerProfile();

我看不出它们有什么区别。所以现在我想知道正确的做法。

您知道,您传递给 AWS 的实际值是 +919829732808,但“+”不能出现在 URL 中,因此它被编码为 %2B。

您可以致电:

customerApi.getCustomerProfile("4", "5", "+919829732808");

但是当你这样称呼它时:

customerApi.getCustomerProfile("4", "5", "%2B919829732808");

Retrofit 进行了额外的 url 编码(% 替换为 %25)并且 AWS 获得 %2B919829732808 而不是 +919829732808。 所以有两种方法——用“+”调用它,Retrofit 将它编码为 %2B,或者用 %2B 调用它,encoded=true。 看看url编码说明:https://www.w3schools.com/tags/ref_urlencode.ASP