Solution 1 :
Your provided json schema is incomplete. Although I have converted it into a java pojo and you can convert these java classes into Kotlin files using your android studio. Make sure you name your package and index class file properly. Also Use Gson Serializers.
-----------------------------------index.Address.java-----------------------------------
package index;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Address {
@SerializedName("address")
@Expose
private String address;
@SerializedName("zipCode")
@Expose
private Object zipCode;
@SerializedName("country")
@Expose
private Country country;
@SerializedName("countryCode")
@Expose
private String countryCode;
@SerializedName("town")
@Expose
private Town town;
@SerializedName("city")
@Expose
private City city;
/**
* No args constructor for use in serialization
*
*/
public Address() {
}
/**
*
* @param zipCode
* @param country
* @param address
* @param town
* @param city
* @param countryCode
*/
public Address(String address, Object zipCode, Country country, String countryCode, Town town, City city) {
super();
this.address = address;
this.zipCode = zipCode;
this.country = country;
this.countryCode = countryCode;
this.town = town;
this.city = city;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Object getZipCode() {
return zipCode;
}
public void setZipCode(Object zipCode) {
this.zipCode = zipCode;
}
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
public Town getTown() {
return town;
}
public void setTown(Town town) {
this.town = town;
}
public City getCity() {
return city;
}
public void setCity(City city) {
this.city = city;
}
}
-----------------------------------index.City.java-----------------------------------
package index;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class City {
@SerializedName("id")
@Expose
private Object id;
@SerializedName("name")
@Expose
private String name;
/**
* No args constructor for use in serialization
*
*/
public City() {
}
/**
*
* @param name
* @param id
*/
public City(Object id, String name) {
super();
this.id = id;
this.name = name;
}
public Object getId() {
return id;
}
public void setId(Object id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
-----------------------------------index.Country.java-----------------------------------
package index;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Country {
@SerializedName("code")
@Expose
private String code;
@SerializedName("name")
@Expose
private String name;
/**
* No args constructor for use in serialization
*
*/
public Country() {
}
/**
*
* @param code
* @param name
*/
public Country(String code, String name) {
super();
this.code = code;
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
-----------------------------------index.Details.java-----------------------------------
package index;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Details {
@SerializedName("id")
@Expose
private Object id;
@SerializedName("slug")
@Expose
private String slug;
@SerializedName("address")
@Expose
private Address address;
/**
* No args constructor for use in serialization
*
*/
public Details() {
}
/**
*
* @param address
* @param id
* @param slug
*/
public Details(Object id, String slug, Address address) {
super();
this.id = id;
this.slug = slug;
this.address = address;
}
public Object getId() {
return id;
}
public void setId(Object id) {
this.id = id;
}
public String getSlug() {
return slug;
}
public void setSlug(String slug) {
this.slug = slug;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
-----------------------------------index.Hotel.java-----------------------------------
package index;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Hotel {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("details")
@Expose
private Details details;
/**
* No args constructor for use in serialization
*
*/
public Hotel() {
}
/**
*
* @param details
* @param id
*/
public Hotel(Integer id, Details details) {
super();
this.id = id;
this.details = details;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Details getDetails() {
return details;
}
public void setDetails(Details details) {
this.details = details;
}
}
-----------------------------------index.Index.java-----------------------------------
package index;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Index {
@SerializedName("responseCode")
@Expose
private Integer responseCode;
@SerializedName("errors")
@Expose
private Object errors;
@SerializedName("message")
@Expose
private Object message;
@SerializedName("result")
@Expose
private Result result;
/**
* No args constructor for use in serialization
*
*/
public Index() {
}
/**
*
* @param result
* @param message
* @param errors
* @param responseCode
*/
public Index(Integer responseCode, Object errors, Object message, Result result) {
super();
this.responseCode = responseCode;
this.errors = errors;
this.message = message;
this.result = result;
}
public Integer getResponseCode() {
return responseCode;
}
public void setResponseCode(Integer responseCode) {
this.responseCode = responseCode;
}
public Object getErrors() {
return errors;
}
public void setErrors(Object errors) {
this.errors = errors;
}
public Object getMessage() {
return message;
}
public void setMessage(Object message) {
this.message = message;
}
public Result getResult() {
return result;
}
public void setResult(Result result) {
this.result = result;
}
}
-----------------------------------index.Offers.java-----------------------------------
package index;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Offers {
@SerializedName("hotels")
@Expose
private List<Hotel> hotels = null;
/**
* No args constructor for use in serialization
*
*/
public Offers() {
}
/**
*
* @param hotels
*/
public Offers(List<Hotel> hotels) {
super();
this.hotels = hotels;
}
public List<Hotel> getHotels() {
return hotels;
}
public void setHotels(List<Hotel> hotels) {
this.hotels = hotels;
}
}
-----------------------------------index.Result.java-----------------------------------
package index;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Result {
@SerializedName("requestId")
@Expose
private String requestId;
@SerializedName("offers")
@Expose
private Offers offers;
/**
* No args constructor for use in serialization
*
*/
public Result() {
}
/**
*
* @param offers
* @param requestId
*/
public Result(String requestId, Offers offers) {
super();
this.requestId = requestId;
this.offers = offers;
}
public String getRequestId() {
return requestId;
}
public void setRequestId(String requestId) {
this.requestId = requestId;
}
public Offers getOffers() {
return offers;
}
public void setOffers(Offers offers) {
this.offers = offers;
}
}
-----------------------------------index.Town.java-----------------------------------
package index;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Town {
@SerializedName("id")
@Expose
private Object id;
@SerializedName("name")
@Expose
private String name;
/**
* No args constructor for use in serialization
*
*/
public Town() {
}
/**
*
* @param name
* @param id
*/
public Town(Object id, String name) {
super();
this.id = id;
this.name = name;
}
public Object getId() {
return id;
}
public void setId(Object id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Problem :
I can access normally like this Json:
[
{
"name": "Algeria",
"capital": "Algiers",
"region": "Africa",
"currency": "DZD",
"flag": "https://raw.githubusercontent.com/atilsamancioglu/IA19-DataSetCountries/master/dza.png",
"language": "Arabic"
},
{
"name": "Andorra",
"capital": "Andorra Vella",
"region": "Europe",
"currency": "EUR",
"flag": "https://raw.githubusercontent.com/atilsamancioglu/IA19-DataSetCountries/master/and.png",
"language": "Catalan"
}
]
But if Json starts like this i can’t access:
{
"responseCode": 200,
"errors": null,
"message": null,
"result": {
"requestId": "360017149b13663c997de5",
"offers": {
"hotels": [
{
"id": 232997,
"details": {
"id": null,
"slug": "grand-park-bodrum-232997",
"address": {
"address": "Peksimet Mah. Bozdağ 12. Sk. No: 7/1 48960 Bodrum-Muğla",
"zipCode": null,
"country": {
"code": "TR",
"name": "Türkiye"
},
"countryCode": "TR",
"town": {
"id": null,
"name": "Bodrum"
},
"city": {
"id": null,
"name": "Muğla"
}
}
My codes like this:
Model.kt
data class Otel(
@SerializedName("errors")
val errors: Any,
@SerializedName("message")
val message: Any,
@SerializedName("responseCode")
val responseCode: Int,
@SerializedName("result")
val result: Result
) {
data class Result(
@SerializedName("filters")
val filters: Filters,
@SerializedName("offers")
val offers: Offers,
@SerializedName("requestId")
val requestId: String,
@SerializedName("sortingMethods")
val sortingMethods: List<SortingMethod>
) { continious like this
OtelAPI
interface OtelAPI {
@GET("otel.json")
fun getOtels(): Single<List<Otel>>
}
OtelAPIService
class OtelAPIService {
private val BASE_URL = "http://www.ipucubilisim.com.tr/"
private val api = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
.create(OtelAPI::class.java)
fun getData() : Single<List<Otel>> {
return api.getOtels()
}
}
FeedViewModel
class FeedViewModel : ViewModel() {
private val otelApiService = OtelAPIService()
private val disposable = CompositeDisposable()
val otels = MutableLiveData<List<Otel>>()
val otelError = MutableLiveData<Boolean>()
val otelLoading = MutableLiveData<Boolean>()
fun refreshData() {
getDataFromAPI()
}
private fun getDataFromAPI() {
otelLoading.value = true
disposable.add(
otelApiService.getData()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(object : DisposableSingleObserver<List<Otel>>(){
override fun onSuccess(t: List<Otel>) {
otels.value = t
otelError.value = false
otelLoading.value = false}
override fun onError(e: Throwable) {
otelLoading.value = false
otelError.value = true
e.printStackTrace()}}))}}
item_otel.xml
<data>
<variable
name="otel"
type="com.berkancalikoglu.projem.model.Otel" />
</data>
<LinearLayout
..
<ImageView
android_id="@+id/imageView"
android_downloadUrl="@{otel.result.offers.Hotel.Details.extra.thumbnailImage}"> </ImageView>
<LinearLayout
..
<TextView
android_id="@+id/name"
android_text="@{otel.result.filters.price.max.name}""> </TextView>
Can you fix my codes? I don’t know how to do. I show you an example of the data I want to access: (By the way this Json starts with {……, not […. )
Comments
Comment posted by ipucubilisim.com.tr/otel.json
I know my shared Json is incomplete because just a sample. Here is the full of Json: