Это простой пример анализа ответа GET с использованием модификации с RX
Предположим, ответ
{
"id": "0001",
"type": "donut",
"name": "Cake",
"image": {
"url": "images/0001.jpg",
"width": 200,
"height": 200
},
"thumbnail": {
"url": "images/thumbnails/0001.jpg",
"width": 32,
"height": 32
}
ApiInterface
@GET("nestedjson5")
Observable<NestedJson5Main> nestedJson5();
Изображение
public class Image {
String url;
int width;
int height;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
Класс модели
public class NestedJson5Main {
String id;
String type;
String name;
Image image;
Image thumbnail;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
}
public Image getThumbnail() {
return thumbnail;
}
public void setThumbnail(Image thumbnail) {
this.thumbnail = thumbnail;
}
}
ViewMOdel Class
public class ParsingVm extends BaseObservable {
Context context;
ApiInterface mApiInterface;
public ParsingVm(Context context) {
this.context = context;
mApiInterface = AppController.getInstance().getmNetComponent().getApiInterface();
}
public Observable<NestedJson5Main> nestedJson5Main() {
return AppController.getInstance().getmNetComponent().getApiInterface()
.nestedJson5()
.subscribeOn(Schedulers.newThread());
}
}
ParsingMain
public class ParsingMain extends AppCompatActivity {
ActivityParsingBinding activityParsingBinding;
ParsingVm parsingVm;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityParsingBinding = DataBindingUtil.setContentView(this, R.layout.activity_parsing);
parsingVm = new ParsingVm(this);
activityParsingBinding.setParsingVm(parsingVm);
receivenesteddjson5();
}
public void receivenesteddjson5() {
parsingVm.nestedJson5Main()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(nestedJson5Main -> {
Log.d(TAG, "receivenesteddjson5: " + nestedJson5Main.getImage().getHeight());
Log.d(TAG, "receivenesteddjson5: " + nestedJson5Main.getThumbnail().getUrl());
}, Throwable -> {
Throwable.printStackTrace();
});
}
}
Я добавил простой ответ, на котором создается и затем наблюдается наблюдаемое.