JSON Parsing with Retrofit

I recently started using Retrofit. I don't know much about this. I ran into this problem and none of the answers answered my problems.

This is the JSON response

{
"results": [
{
"description_eng": "This is second time testing",
"img_url": "-",
"title_eng": "Second test"
},
{
"description_eng": "Hello 1 2 3, I am testing.",
"img_url": "https://fbcdn-sphotos-f-a.akamaihd.net/hphotos-ak-xpa1/t31.0-8/s720x720/10838273_816509855058935_6428556113200361121_o.jpg",
"title_eng": "Test"
}
]
}

      

This is a filtration class

public class Feed {
    public List<Results> results;
    class Results{
        String description_eng,img_url,title_eng;
    }
}

      

This is the interface

public interface GetApi {
    @GET("/api.json")
    public void getData(Callback<List<Feed>> response);
}

      

I got it json_illegal_syntax Exception

.

+3


source to share


3 answers


This is how I solved this problem.

Feed.class

public class Feed{
    private List<Result> results;

    public Feed(){

    }
    public List<Result> getFeed(){
        return this.results;
    }
    public void setFeed(List<Result> results) {
        this.results = results;
    }
}

      

Result.class



public class Result{
    private String description_eng;
    private String img_url;
    private String title_eng;

    public Result(){}
    //getters and setters
    }

      

GetApi.class

public interface GetApi {
    @GET("/api.json")
    public void getData(Callback<Feed> response);
}

      

Thanks for answering them.: D

+3


source


Retrofit uses Gson by default to convert HTTP bodies to and from JSON. If you want to specify behavior other than the default Gson values โ€‹โ€‹(eg naming policies, date formats, custom types), when creating the RestAdapter, provide a new Gson instance with the desired behavior.

Gson cannot automatically deserialize pure inner classes, as their no-args constructor also needs a reference to the containing object, which is not available during deserialization. You can solve this problem either by making the inner class static or by providing its own InstanceCreator instance. Here's an example:

public class A { 
  public String a; 

  class B { 

    public String b; 

    public B() {
      // No args constructor for B
    }
  } 
}

      



NOTE. The above class B cannot (by default) be serialized with Gson.

You should read more about the GSON library

+1


source


@Ye Min Htut It's actually even better to write a Feed class with generics.

public class FeedList<T>{
   private List<T> feeds;
   public Feed() {

   }
   public List<T> getFeed(){
       return this.feeds;
   }
   public void setFeed(List<T> results) {
       this.feeds = feeds;
   }
}

      

and if there is something like this with only one object, you can remove part of the list and get / set a separate object.

Now you can call FeedList<Result>

or whatever object you want.

GetApi.class

public interface GetApi {
@GET("/api.json")
    public void getData(Callback<FeedList<Result>> response);
} 

      

+1


source







All Articles