[英]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
com.squareup.retrofit2:converter-gson
GsonConverterFactory 可能会加下划线,因为您没有添加依赖com.squareup.retrofit2:converter-gson
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.