【问题标题】:Unable to create converter for class com.squareup.okhttp.ResponseBody无法为 com.squareup.okhttp.ResponseBody 类创建转换器
【发布时间】:2016-11-17 00:24:19
【问题描述】:

改造文档说:

“Retrofit默认只能将HTTP body反序列化成OkHttp的ResponseBody...可以添加转换器支持其他类型”

这意味着我应该能够使用 GSON 转换器进行 api 调用,并以“ResponseBody”对象的形式获得我的响应。

但我仍然收到错误

java.lang.IllegalArgumentException: Unable to create converter for class com.squareup.okhttp.ResponseBody

这是我的代码

    @GET("v1/search")
    Call<ResponseBody> getArtists(@Query("q") String name, @Query("type") String searchType);
    Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl("https://api.spotify.com/")
                    .build();

    api = retrofit.create(SpotifyApi.class);

    api.getArtists(searchBox.getText().toString(), "artist")
                             .enqueue(new Callback<ResponseBody>() {
                                 @Override
                                 public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

                                 }

                                 @Override
                                 public void onFailure(Call<ResponseBody> call, Throwable t) {

                                 }
                             });

基本上,我希望能够以最纯粹/最简单的形式使用 Retrofit,并获得基本/原始响应。这不是用于真正的应用程序,而是用于实验。

【问题讨论】:

    标签: java android retrofit


    【解决方案1】:

    您需要使用 OkHttp 3.x 中的okhttp3.ResponseBody(Retrofit 依赖于该版本)。该错误消息表明您使用的是 OkHttp 2.x 中的类型。

    【讨论】:

    • 天哪,你太棒了!如果您能解释一下您是如何解决这个问题的,我将不胜感激
    • 我写了 Retrofit 2 并为 OkHttp 3 做贡献:)
    • 是的,我写完之后马上就明白了哈哈,非常感谢你们,你们是最棒的!
    猜你喜欢
    • 2017-01-12
    • 2019-07-25
    • 2021-11-22
    • 2016-06-23
    • 2021-06-12
    • 1970-01-01
    • 2014-11-30
    • 2017-12-23
    • 2020-10-30
    相关资源
    最近更新 更多