繁体   English   中英

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

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

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

我想使用方法 ONE 获取数据,但该方法不起作用。

请考虑基础 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 调用它并编码=true。 看看 url 编码说明: https://www.w3schools.com/tags/ref_urlencode.ASP

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM