0

I am new in JSON parsing and trying to parse the following JSON:

[
 {
"id" : 1,
"title" : {
    "rendered": "a link"
 },
"categories": [ 4,9,11 ],
"links":{
        "featuredmedia":[
        {
          "href": link
        }
           ]
    }
},
...
]

My Interface is:

public interface MediaAPI {
    @GET("Media")
    Call<LinkList> getDetails();
}

My model classes are:

public class LinkList {
    private List<Links> links;
    // getter and setter    
}

...

public class Links {
  private List<Featuredmedia> Featured = new ArrayList<Featuredmedia>();
  // getter and setter 
 }

...

public class Featuredmedia {
    private String href;
    // getter and setter    
}

and Client code is:

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(ROOT_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    MediaAPI service = retrofit.create(MediaAPI.class);

    Call<LinkList> call = service.getDetails();
    call.enqueue(new Callback<LinkList>() {
        @Override
        public void onResponse(Call<LinkList> call, Response<LinkList> response) {
            if(response.isSuccessful()){                   
                successToast();
            }
            else {                                      
                failToast();
            }
        }

        @Override
        public void onFailure(Call<LinkList> call, Throwable t) {               
            Log.d("Failed", t.getMessage());
            showToast();

        }
    });

I only need to get the link inside "featuredmedia" so I only included those in the models. I also got some idea about the error from here but the error still there.

Any suggestion how to solve this will be great help.

Community
  • 1
  • 1
Sanzi
  • 13
  • 1
  • 2
  • Instead of Call getDetails(), try Call getDetails() – Yasir Tahir Apr 26 '16 at 11:31
  • To better understand JSON, please [read this link](http://www.json.org/), and check these tools: [formatter](https://jsonformatter.curiousconcept.com) and [editor](http://www.jsoneditoronline.org/) that greatly help (at least me) validate and "read" what the Data is, and what it means... Basically `[...]`are Arrays, and `{...}` are Objects – Bonatti Apr 26 '16 at 11:43

2 Answers2

0

Change the Links model like this .

public class Links {
  private List<Featuredmedia> featuredmedia = new ArrayList<Featuredmedia>();
  // getter and setter 
 }

And also LinkList should be like this .

public class LinkList {
    private Links links;
    // getter and setter    
}
Zahidul Islam
  • 3,180
  • 1
  • 25
  • 35
0

Gson will take care of Deserializing the json to your LinksList and expects an object for this to work.

Your model basically expects something like :

{
   links: [...]
}

Since your Response Json is an array, you want to have a model representing this: any Array or List will do.

So instead of using LinkList, just use e.g. Call<List<Links>>

J. Dow
  • 563
  • 3
  • 13