简体   繁体   English

无法在 Android Retrofit 中为我的 class 创建转换器

[英]Unable to create converter for my class in Android Retrofit

I am trying to develop clent-server application with Retrofit.我正在尝试使用 Retrofit 开发客户端服务器应用程序。 My application sends to server json with a string "image" and responses a json with a string with field "name".我的应用程序使用字符串“image”发送到服务器 json,并使用带有字段“name”的字符串响应 json。

My API:我的 API:

public interface API {
    @FormUrlEncoded
    @POST("/api/classification/imagenet")
    Call<GestureJson> getName(@Body ImageJson json);
}

ImageJson:图像Json:

public class ImageJson {
    public String imageString;
}

NameJson:姓名Json:

public class NameJson {
    public int gestureNumber;
}

When user pressed on button in Main Activity MyVoid is called (url is already known):当用户按下 Main Activity MyVoid 中的按钮时(url 已知):

public void MyVoid() {
        String requestUrl = url;
        Retrofit retrofit = new Retrofit.Builder()
                .addCallAdapterFactory(GsonConverterFactory.create())
                .baseUrl(requestUrl)
                .build();
        API api = retrofit.create(API.class);
        ImageJson json = new ImageJson();
        json.imageString = image_uri.toString();
        Call<GestureJson> call = api.getName(json);
        call.enqueue(new Callback<GestureJson>() {
            @Override
            public void onResponse(Call<GestureJson> call, Response<GestureJson> response) {
                if (response.isSuccessful()) {
                    status = RESPONSE_SUCCESS;
                } else {
                    status = RESPONSE_FAIL;
                }
            }

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

            }
        });

I have three problems:我有三个问题:

1) I don't know what's the difference between Retrofit and Retrofit2. 1)我不知道Retrofit和Retrofit2有什么区别。 What is better to use?用什么比较好?

2).addCallAdapterFactory(GsonConverterFactory.create()) underlined how wrong (in Retorfit and Retrofit2). 2).addCallAdapterFactory(GsonConverterFactory.create()) 强调了错误的程度(在 Retorfit 和 Retrofit2 中)。

3) I can compile application if i delete.addCallAdapterFactory(GsonConverterFactory.create()). 3) 如果我删除.addCallAdapterFactory(GsonConverterFactory.create()),我可以编译应用程序。 But I have a problem:但我有一个问题:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.opencvproject, PID: 17742
java.lang.IllegalArgumentException: Unable to create converter for class com.example.opencvproject.GestureJson
    for method API.getGesture
  1. Retrofit2 is better because Retrofit outdated (last release was on 2014 https://github.com/square/retrofit/releases/tag/parent-1.6.0 ). Retrofit2 更好,因为 Retrofit 已过时(最后一个版本是在 2014 年https://github.com/square/retrofit/releases/tag/parent-1.6.0 )。 Number 2 in name is just a library version.名称中的 2 号只是一个库版本。
  2. GsonConverterFactory may be underlined because you did't add dependency com.squareup.retrofit2:converter-gson GsonConverterFactory 可能会加下划线,因为您没有添加依赖com.squareup.retrofit2:converter-gson
  3. If you delete addCallAdapterFactory(GsonConverterFactory.create()) then Retrofit would't know how to deserialize json to objects.如果删除addCallAdapterFactory(GsonConverterFactory.create())则 Retrofit 将不知道如何将 json 反序列化为对象。 GsonConverterFactory use Gson libarary ( https://github.com/google/gson ) under the hood to deserialize server json responses. GsonConverterFactory 在后台使用 Gson 库( https://github.com/google/gson )反序列化服务器 json 响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 改造:无法为类创建@Body 转换器 - Retrofit: Unable to create @Body converter for class Retrofit和Rxjava“无法为java.util.List创建转换器 <Model> ” - Retrofit and Rxjava “Unable to create converter for java.util.List<Model>” 无法在Android中使用Retrofit和Java创建POJO - Unable to create POJO with Retrofit and Java in android 无法为java.util.List创建转换器Retrofit 2.0.0-beta2 - Unable to create converter for java.util.List Retrofit 2.0.0-beta2 无法为类com.squareup.okhttp.ResponseBody创建转换器 - Unable to create converter for class com.squareup.okhttp.ResponseBody (改造)找不到类崩溃应用程序的转换器 - (Retrofit) Could not locate converter for class crashing app 改造:无法创建适配器 - Retrofit :Unable to create adapter java android Eclipse Retrofit 2.0 转换为 Dalvik 格式失败:无法执行 dex:多个 dex 文件定义了 Lretrofit2/Converter - java android Eclipse retrofit 2.0 Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define Lretrofit2/Converter 我的JSON输出有什么问题? retrofit.converter.ConversionException - What's wrong with my JSON Output? retrofit.converter.ConversionException Android:包含用于Retrofit2的SimpleXML转换器时,ClassNotFoundException - Android: ClassNotFoundException when including SimpleXML converter for Retrofit2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM