【问题标题】:Add Multiple @Path with retrofit call使用改造调用添加多个 @Path
【发布时间】:2021-02-15 06:39:07
【问题描述】:

我有一个 api 请求,我需要在 url 中传递多个@Path,我这样做了,但我一直收到错误,我想就这个问题获得一些帮助,提前谢谢你

  • 这是网址示例
    https://api.site.dev/api/v2/entries/en_US/hello
    
  • 这是改造设置

  @Singleton
  @Provides
  fun provideRetrofitInstance(): ApiInterface {
      val httpLoggingInterceptor = HttpLoggingInterceptor()
      val interceptor = httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC)
      val okHttp = OkHttpClient.Builder()
              .addInterceptor(interceptor)
              .build()
      return Retrofit.Builder()
              .baseUrl("https://api.site.dev/api/v2/entries/")
              .addConverterFactory(GsonConverterFactory.create())
              .client(okHttp)
              .build()
              .create(ApiInterface::class.java)
  }

  • 这是我的改造电话
 Error Unable to create call adapter for class com.dic.mydictionnary.models.DictionnaryModelItem
       for method ApiInterface.getDictionnaryWord

*这是我的 apiInterfac

@GET("{language_code}/{word}")
   fun getDictionnaryWord(
           @Path("language_code") language : String,
           @Path("word") word : String,
   ) : DictionnaryModelItem

}

【问题讨论】:

    标签: android kotlin retrofit


    【解决方案1】:

    看起来 Retrofit 正在尝试找到一种方法来为您的服务接口创建 DictionnaryModelItem。您需要将其更改为:

       @GET("{language_code}/{word}")
       suspend fun getDictionnaryWord(
               @Path("language_code") language : String,
               @Path("word") word : String,
       ) : Response<DictionnaryModelItem>
    

    【讨论】:

    • 是的,我确实想通了,因为我使用了 flow api,所以我错过了关键字暂停,但您的帖子实际上显示了正确的响应,感谢您的时间和精力
    猜你喜欢
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多