3

I started using Retrofit recently. I don't know much about it. I have googled this issue and no answers suite my problem.

This is 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 Feed 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 json_illegal_syntax Exception.

Ye Min Htut
  • 2,904
  • 15
  • 28

4 Answers4

3

This is how I solved this problem, by creating empty constructors.

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);
}
Ye Min Htut
  • 2,904
  • 15
  • 28
  • I am having a similar problem: http://stackoverflow.com/questions/39310585/cannot-parse-json-response-object/39314214#39314214 – tccpg288 Sep 08 '16 at 02:36
1

Retrofit uses Gson by default to convert HTTP bodies to and from JSON. If you want to specify behavior that is different from Gson's defaults (e.g. naming policies, date formats, custom types), provide a new Gson instance with your desired behavior when building a RestAdapter.

Gson can not automatically deserialize the pure inner classes since their no-args constructor also need a reference to the containing Object which is not available at the time of deserialization. You can address this problem by either making the inner class static or by providing a custom InstanceCreator for it. Here is 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 can not (by default) be serialized with Gson.

You should read more about GSON library

Holoceo
  • 168
  • 2
  • 7
1

@Ye Min Htut Actually, even better is to write 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 similar with only one object, than you can remove List part and get/set single object.

Now you can call FeedList<Result> or with whatever object you want.

GetApi.class

public interface GetApi {
@GET("/api.json")
    public void getData(Callback<FeedList<Result>> response);
} 
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
NixSam
  • 615
  • 6
  • 20
0

@Ye Min Htut I will suggest you to make Model/POJO classes using ROBOPOJO Generator It will generate All model classes for you, you don't need to make it by your self, Will also help you in future while creating model class. It just need JSON string and click will make your job done

Jaykishan Sewak
  • 822
  • 6
  • 13