简体   繁体   中英

Why does Retrofit & Gson return a List of LinkedTreeMap instead of my expected type?

I am trying to retrieve a list of objects via Retrofit:

public interface TemplateApi {
    @GET("/api/templates")
    Observable<List<TemplateDto>> getList();
}

Here the Dto:

public class TemplateDto {
    public String id;
    public String name;

    @Override
    public String toString() {
        return this.name;
    }
}

Instead of the expected List I get a

List<LinkedTreeMap>

which leads to weird side effects (in my case toString() returns not what I would expect)

Ok I found it out - Android really does not have the flattest learning curve...

The issue was not with Retrofit or Gson itself but it was the minifier (Proguard). Proguard needs to be configured in a file which is called "proguard-rules.pro" by default.

Add the following lines:

-keepattributes Signature
-keepattributes *Annotation*
-keep class com.myproject.dtos.** { *; }

Thanks to this answer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM