0

I have been trying to find a tutorial for Moshi library in android java for parsing some complicated api but am unable to do so.... any help is appriciated ...

Api sample Value

{
"kind": "youtube#searchListResponse",
"etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/VZYjjnY2DRtcVQGjTcTJVE_YOvM\"",
"nextPageToken": "CAUQAA",
"regionCode": "IN",
"pageInfo": {
    "totalResults": 1000000,
    "resultsPerPage": 5
},
"items": [
    {
        "kind": "youtube#searchResult",
        "etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/njM_Wom6sRs6l5kN528JyqirbzU\"",
        "id": {
            "kind": "youtube#video",
            "videoId": "8-mloCL49vs"
        },
        "snippet": {
            "publishedAt": "2018-05-11T04:00:00.000Z",
            "channelId": "UCz9yS18zJGQObwUL_K-ICnw",
            "title": "Karol G - Mi Cama",
            "description": "Music video by Karol G performing Mi Cama. © 2018 UMG Recordings, Inc. http://vevo.ly/rsmwbJ.",
            "thumbnails": {
                "default": {
                    "url": "https://i.ytimg.com/vi/8-mloCL49vs/default.jpg",
                    "width": 120,
                    "height": 90
                },
                "medium": {
                    "url": "https://i.ytimg.com/vi/8-mloCL49vs/mqdefault.jpg",
                    "width": 320,
                    "height": 180
                },
                "high": {
                    "url": "https://i.ytimg.com/vi/8-mloCL49vs/hqdefault.jpg",
                    "width": 480,
                    "height": 360
                }
            },
            "channelTitle": "KarolGVEVO",
            "liveBroadcastContent": "none"
        }
    }
]}

this is the code i found & was trying

 Moshi moshi = new Moshi.Builder().build();
 JsonAdapter<Hero> adapter = moshi.adapter(Hero.class);                    
 Hero hero = adapter.fromJson(run(strings[0]));

i am getting app crashes & the error is

 Caused by: java.lang.IllegalArgumentException: Platform java.util.ArrayList<com.example.Inside> (with no annotations) requires explicit JsonAdapter to be registered

EDIT :

i have added the code for my Hero class

public class Hero {

    private List<Inside> items;
    private String kind;
    private String etag;
    private String nextPageToken;
    private String regionCode;
    private PAGEINFO pageInfo;

    public Hero() {
    }

    public Hero(List<Inside> items, String kind, String etag, String nextPageToken, String regionCode, PAGEINFO pageInfo) {
        this.items = items;
        this.kind = kind;
        this.etag = etag;
        this.nextPageToken = nextPageToken;
        this.regionCode = regionCode;
        this.pageInfo = pageInfo;
    }

    //setter getters for all elements

}

& some idea about RealmListJsonAdapter is also appreciated .

thnx in advance

user2340612
  • 10,053
  • 4
  • 41
  • 66
Anjani Mittal
  • 507
  • 1
  • 7
  • 19
  • 1
    We need to see the `Hero` model class. As the error states, you shouldn't tie yourself to the platform `ArrayList` concrete type. Use `List` instead. – Eric Cochran Aug 03 '18 at 20:03
  • @EricCochran i have added the class in edit – Anjani Mittal Aug 06 '18 at 04:07
  • We still need to know what `Inside` and `PAGEINFO` look like. Moshi is deserializing those types, too. Some type in there has an ArrayList field. – Eric Cochran Aug 06 '18 at 18:59
  • As Eric said, the issue is that Moshi doesn't know how to deserialize `Inside` (and you'll face the same issue with `PAGEINFO` most likely), so you need to add a custom `Adapter` to your `Moshi` instance (through a `Moshi.Builder` object). Check [this example](https://github.com/square/moshi#custom-type-adapters) – user2340612 Aug 07 '18 at 12:22
  • thnx @EricCochran ... problem solved , i made a few changes & now its working fine... – Anjani Mittal Aug 08 '18 at 08:52

0 Answers0