【问题标题】:Retrofit not write all request's data改造不写所有请求的数据
【发布时间】:2018-10-30 23:32:41
【问题描述】:

我在改装方面遇到了问题。 我的应用程序有一个名为Request 的按钮。当用户单击Request 按钮时,retrofit 会执行请求。 你能解释一下为什么改造不写第二个请求的所有数据吗?

有改造配置:

改造配置:

Retrofit retrofit = new Retrofit.Builder()
             .baseUrl(BuildConfig.API_BASE_URL)
             .client(client)
             .addConverterFactory(gson)
             .addCallAdapterFactory(rxAdapter)
             .build();

HttpClient

HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
     logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
     TokenInterceptor tokenInterceptor = new TokenInterceptor();
     return new OkHttpClient.Builder()
             .connectTimeout(20, TimeUnit.SECONDS)
             .readTimeout(30, TimeUnit.SECONDS)
             .addInterceptor(tokenInterceptor)
             .addInterceptor(logging)
             .cache(cache)
             .build();

有来自改造的http日志:

  • 第一次点击,改造写入155088字节正文,服务器返回403
  • 第二次点击,改造不写所有数据(只有2023字节的正文)。客户端收到Unable to resolve host 错误。

    10-30 21:01:08.858 14479-14626/com.example.retrofit D/OkHttp: --> POST https://com.example.retrofit/v1/symptom/update/257http/1.1 (155088 字节正文)10-30 21:01:09.190 14479-14626/com.example.retrofit D/OkHttp: https://com.example.retrofit/v1/symptom/update/257 (332 毫秒,560 字节正文)10-30 21:01:23.109 14479-14573/com.example.retrofit D/OkHttp: --> POST https://com.example.retrofit/v1/symptom/update/257 http/1.1 (2023 字节 正文) 10-30 21:13:27.578 14479-14573/com.example.retrofit D/OkHttp:

【问题讨论】:

    标签: android retrofit retrofit2


    【解决方案1】:

    错误Unable to resolve host "com.example.retrofit": No address associated with hostname 表示您没有互联网连接,因此客户端无法连接到该网站,或者该网站本身不存在。在您的情况下,看起来肯定没有名为 com.example.retrofit 的网站。

    为了更好地为您服务,它需要连接到您的主机后端网站或您的本地主机网站地址,该地址进入.baseUrl(websiteUrl: String)方法

    BuildConfig.API_BASE_URL 不适合。

    要开始改造,您应该免费使用第三方 API,例如 GithubDarkSky 等。

    按照那里的说明使用 API,然后看看 retrofit 的魔力

    【讨论】:

    • 设备仍处于连接状态。第一次有效,第二次无效。
    • BuildConfig.API_BASE_URLbuild.gradle 上的常量值
    • com.example.retrofit 这是在stackoverflow上询问的假网址。
    • 确保第二次连接是同一个网站,因为你得到的错误只会出现在我提到的两种情况下,没有互联网场景和没有网站主机场景。如果你说你有互联网,那一定是你试图 ping 的网站有问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-08
    • 2016-07-28
    • 1970-01-01
    • 2018-08-30
    • 1970-01-01
    • 2016-02-08
    • 2019-05-15
    相关资源
    最近更新 更多