【问题标题】:What is the right way to pass the parameter in the retrofit GET call in Android?在 Android 的改造 GET 调用中传递参数的正确方法是什么?
【发布时间】:2020-08-08 17:41:46
【问题描述】:

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

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

请考虑基本网址没有问题<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();

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

【问题讨论】:

  • 我认为 %2B 正在发生一些编码,最好的方法是查看服务器上的日志(或调用本地服务器并查看调用的差异)。看一下“编码”参数:stackoverflow.com/a/51273510/2842750
  • 尝试使用Chucker库拦截请求查看全图
  • @PavelBiryukov 这解决了这个问题。我不是那个。
  • 酷!我可以写一个答案并将其标记为答案吗? :)
  • @PavelBiryukov 当然。另外请务必解释它的原因以及为什么在查询参数中执行encoding=true 可以解决问题。

标签: android amazon-web-services get retrofit rx-java


【解决方案1】:

您会看到,您传递给 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-28
    • 1970-01-01
    相关资源
    最近更新 更多