Java (改造)找不到类崩溃应用程序的转换器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32343183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:22:26  来源:igfitidea点击:

(Retrofit) Could not locate converter for class crashing app

javaandroidretrofit

提问by Orbit

So Retrofit 2.0.0 was recently released and theres not really any updated examples on how to use it, but im trying to implement it for a basic API call. Im getting a

所以最近发布了 Retrofit 2.0.0,并没有任何关于如何使用它的更新示例,但我正在尝试为基本的 API 调用实现它。我得到一个

java.lang.IllegalArgumentException: Unable to create converter for class` 

caused by

造成的

Caused by: java.lang.IllegalArgumentException: Could not locate converter for class orbyt.app.dataclass. Tried:
* retrofit.OkHttpBodyConverterFactory

When trying to make the api call.

尝试进行 api 调用时。

采纳答案by YacSrk

I was facing the same issue. I fixed it by adding :

我面临着同样的问题。我通过添加来修复它:

compile 'com.squareup.retrofit2:converter-gson:<latest-version>'

to my build.gradle

到我的 build.gradle

Then specify the converter when creating my Retrofit instance.

然后在创建我的 Retrofit 实例时指定转换器。

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Constants.API_BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

回答by Ajit Kumar Dubey

In Retrofit 2.0, Converter is not included in the package and when you are using Retrofit 2.0 Make Sure follow new URL pattern

在 Retrofit 2.0 中,转换器不包含在包中,当您使用 Retrofit 2.0 时,请确保遵循新的 URL 模式

Base URL: always ends with /

基本 URL:总是以 / 结尾

@Url: DO NOT start with /

@Url:不要以 / 开头

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(Constants.API_BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build();

For more information about 2.0 Follow this link Retrofit 2.0: The biggest update

有关 2.0 的更多信息,请点击此链接Retrofit 2.0:最大的更新

And also update build.gradle.

并更新 build.gradle。

implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"

And add the extension in project level build.gradle file

并在项目级 build.gradle 文件中添加扩展名

ext {
retrofit_version= "2.x.x"
}

回答by Neo

For Retrofit V2 add the following repositories -

对于 Retrofit V2,添加以下存储库 -

compile 'com.squareup.retrofit2:retrofit:2.0.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.0'

Now use below code -

现在使用下面的代码 -

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(API_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

Hope it will help :)

希望它会有所帮助:)

回答by joe

In the latest Retrofit 2.0,you should import the latest version :

在最新的 Retrofit 2.0 中,您应该导入最新版本:

compile 'com.squareup.retrofit2:retrofit:2.0.0'
compile 'com.squareup.retrofit2:converter-gson:2.0.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0'

Be careful call baseUrl(),at v2.0,it should be end of "/",and at the method ,you would't start thr url with"/"

小心调用baseUrl(),在v2.0,它应该以“/”结尾,并且在方法中,你不会以“/”开始thr url

@POST("classes/info")
Call<ContactBean> insertInfo(@Body ContactBean bean);

And you can see Retrofitto get more info! Hope help !

您可以查看Retrofit以获取更多信息!希望有帮助!

回答by Abilash Rajasekaran

Change retrofit version accordingly

相应地更改改造版本

For me below dependency was there already

对我来说,下面的依赖已经存在

compile 'com.squareup.retrofit2:retrofit:2.0.2'

For gson 2.0.2 I changed

对于 gson 2.0.2 我改变了

compile 'com.squareup.retrofit2:converter-gson:2.0.2'

Then add

然后加

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Constants.API_BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

回答by CoolMind

In my case(Kotlin with coroutines) I received the exception:

我的情况下(带有协程的 Kotlin),我收到了异常:

Unable to create converter for retrofit2.Call

for method Queries.exportPdf.

Caused by: java.lang.IllegalArgumentException: Could not locate ResponseBody converter for retrofit2.Call

无法为改造 2.Call 创建转换器

对于方法 Queries.exportPdf。

引起:java.lang.IllegalArgumentException:无法找到retrofit2.Call的ResponseBody转换器

A problem was in a request:

一个请求出现问题:

@FormUrlEncoded
@Streaming
@POST("export-pdf/")
suspend fun exportPdf(
    @Field("token") token: String
): Call<ResponseBody>

Removed suspendfrom definition and exceptions disappeared.

suspend从定义中删除,异常消失。