0

This is my Json Data

[
  {
    "id": 1,
    "name": "ANDY",
    "Game": {
      "car": "1 Item",
      "plane": "1 Item"
    },
    "location": {
      "home": 5.555,
      "office": 150.316
    }
  }
}

Here is my calling API :

@GET("/sample.json")
Observable<Response> getAppTours(@Header("If-None-Match") String etag);

How can I access the game and get the car & plane location : home & office ?

I am using Retrofit and Ormlite. I keep getting error on Game and Location while I am adding the @DATABASEFIELD Game game;

The error is said :

Attempt to invoke interface method 'com.j256.ormlite.stmt.QueryBuilder com.j256.ormlite.dao.Dao.queryBuilder()' on a null object reference

Shree Krishna
  • 8,474
  • 6
  • 40
  • 68

2 Answers2

0

It's simple to to get Nested objects if you use core class library to read Json. But Using retrofit you have to create another POJO (model) for game object. Have a brief look.

Community
  • 1
  • 1
Shree Krishna
  • 8,474
  • 6
  • 40
  • 68
0

Copy/paste this JSON as input of this page http://www.jsonschema2pojo.org/:

[
  {
    "id": 1,
    "name": "ANDY",
    "Game": {
      "car": "1 Item",
      "plane": "1 Item"
    },
    "location": {
      "home": 5.555,
      "office": 150.316
    }
  }
]

You had a } instead of a ] to close it up, on biggies. Now change the source type to JSON and select your converter accordingly.

You can check the Retrofit's available converters in their main documentation almost at the bottom of the page http://square.github.io/retrofit/.

Make sure you add the needed line to your gradle, being that something similar to:

Gson: com.squareup.retrofit2:converter-gson
Jackson: com.squareup.retrofit2:converter-jackson
Moshi: com.squareup.retrofit2:converter-moshi
Protobuf: com.squareup.retrofit2:converter-protobuf
Wire: com.squareup.retrofit2:converter-wire

Now from jsonschema2pojo click on "Preview" or "Zip" you will then be able to download your new model. Copy those classes to your project.

Your new Retrofit interface will look something like:

public interface YourService {
  @GET("your/url")
  Call<Yourclass> listStuff();
}

Then, after that, you will be able to make the HTTP request with:

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl(API_URL)
    .addConverterFactory(GsonConverterFactory.create()) //Converter
    .build();

// Create an instance of our GitHub API interface.
YourService service = retrofit.create(YourService.class);

// Create a call instance for looking up Retrofit contributors.
Call<Yourclass> call = service.listStuff();

And get your games and locations with:

Yourclass myclass = call.execute().body();
Game game = myclass.getGame();
Location location = myclass.getLocation();

Check these if you want to get more details:

https://github.com/square/retrofit/blob/master/samples/src/main/java/com/example/retrofit/SimpleService.java

http://square.github.io/retrofit/

https://github.com/joelittlejohn/jsonschema2pojo

Evin1_
  • 12,292
  • 9
  • 45
  • 47