本文介绍了无法为java.util.List Retrofit 2.0.0-beta2创建转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在做一个GET请求,但是我收到了这个错误:

I'm just doing a GET request, but I'm getting this error:

这是因为这行代码:

Call<List<Store>> call = subpriseAPI.listStores(response);

所以我尝试使用这行代码来查看它是什么类型:

So I had tried with this line of code to see what type it is:

System.out.println(subpriseAPI.listStores(response).getClass().toString());

但是我得到了同样的错误,所以它不会让我知道它是什么类型。在下面,您可以看到我的代码。

But then I get the same error so it doesn't let me know what type it is. Here below you can see my code.

StoreService.java:

StoreService.java:

public class StoreService {

    public static final String BASE_URL = "http://getairport.com/subprise/";
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .build();

    SubpriseAPI subpriseAPI = retrofit.create(SubpriseAPI.class);
    String response = "";

    public List<Store> getSubprises() {

        Call<List<Store>> call = subpriseAPI.listStores(response);

        try {
            List<Store> listStores = call.execute().body();

            System.out.println("liststore "+ listStores.iterator().next());
            return listStores;
        } catch (IOException e) {
            // handle errors
        }
        return null;
    }
}

SubpriseAPI.java:

SubpriseAPI.java:

public interface SubpriseAPI {
    @GET("api/locations/get")
    Call<List<Store>> listStores(@Path("store") String store);
}

Store.java:

Store.java:

public class Store {
    String name;
}

我正在使用Retrofit版本2.0.0-beta2。

I'm using Retrofit version 2.0.0-beta2.

推荐答案

在2+版本中,您需要通知转换器

In the 2+ version you need to inform the Converter

默认情况下,Retrofit只能将HTTP主体反序列化为OkHttp的
ResponseBody类型,它只能接受
的RequestBody类型@Body。

By default, Retrofit can only deserialize HTTP bodies into OkHttp's ResponseBody type and it can only accept its RequestBody type for @Body.

可以添加转换器以支持其他类型。六个兄弟模块
适应流行的序列化库以方便您使用。

Converters can be added to support other types. Six sibling modules adapt popular serialization libraries for your convenience.

Gson:com.squareup.retrofit:converter-gson Jackson:com.squareup.retrofit:converter -jackson

Moshi:com.squareup.retrofit:converter-moshi

Protobuf:com.squareup.retrofit:converter-protobuf

Wire:com.squareup .retrofit:converter-wire

简单XML:com.squareup.retrofit:converter-simplexml

Gson: com.squareup.retrofit:converter-gson Jackson: com.squareup.retrofit:converter-jackson
Moshi: com.squareup.retrofit:converter-moshi
Protobuf: com.squareup.retrofit:converter-protobuf
Wire: com.squareup.retrofit:converter-wire
Simple XML: com.squareup.retrofit:converter-simplexml



// Square libs, consume Rest API
compile 'com.squareup.retrofit:retrofit:2.0.0-beta1'
compile 'com.squareup.okhttp:okhttp:2.4.0'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta1'

所以,

String baseUrl = "" ;
Retrofit client = new Retrofit.Builder()
    .baseUrl(baseUrl)
    .addConverterFactory(GsonConverterFactory.create())
    .build();

这篇关于无法为java.util.List Retrofit 2.0.0-beta2创建转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 23:54