I got this data using the Retrofit library:
[
{
"countryId": "1472",
"countryName": "{"ar": "ألمانيا", "default": "Germany"}",
"image": "a304035c3dcb42cd990bb69b2f03e31f.png"
},
{
"countryId": "1473",
"countryName": "{"ar": "إيطاليا", "default": "Italy"}",
"image": "5b3ae479ada846e98309ed978c2707b5.png"
},
{
"countryId": "1474",
"countryName": "{"ar": "هولندا", "default": "Netherlands"}",
"image": "d810f9ab22434b4da08b838e72add09d.png"
},
{
"countryId": "1475",
"countryName": "{"ar": "بولندا", "default": "Poland"}",
"image": "d8c4de2a11ca45759089fec204af9659.png"
},
{
"countryId": "1476",
"countryName": "{"ar": "رومانيا", "default": "Romania"}",
"image": "47efdea8456244a5b9aae7132fca7418.png"
},
{
"countryId": "1477",
"countryName": "{"ar": "روسيا", "default": "Russia"}",
"image": "7163f60a1c494e1b9f782edd3ecabd31.png"
},
{
"countryId": "1478",
"countryName": "{"ar": "إسبانيا", "default": "Spain"}",
"image": "52fe49f594074b078fd5d8c9625018ee.png"
},
{
"countryId": "1479",
"countryName": "{"ar": "اوكرانيا", "default": "Ukraine"}",
"image": "28581f7e4f324d938e0b109f7ee9203e.png"
},
{
"countryId": "1480",
"countryName": "{"ar": "المملكة المتحدة", "default": "United Kingdom"}",
"image": "e7a87ff0caa241559f6c2559cc8606c3.png"
},
{
"countryId": "2147483647",
"countryName": "{"ar": "فرنسا", "default": "France"}",
"image": "3830917201c74fc9b6b4ed0ddfdd4866.png"
}
]
This is the code that gets the data:
Retrofit retrofit = new Retrofit.Builder().baseUrl(Constants.baseURL).addConverterFactory(GsonConverterFactory.create()).build();
TestInterface testInterface = retrofit.create(TestInterface.class);
testInterface.getCountries().enqueue(new Callback < List < CountriesModel >> () {
@Override
public void onResponse(@NonNull Call < List < CountriesModel >> call, @NonNull Response < List < CountriesModel >> response) {
if (response.body() == null)
return;
for (CountriesModel item: response.body()) {
Chip chip = new Chip(requireActivity());
chip.setText(item.getCountryName());
fragmentSelectCountryBinding.fragmentSelectCountryChipGroup281.addView(chip);
}
}
@Override
public void onFailure(@NonNull Call < List < CountriesModel >> call, @NonNull Throwable t) {
Log.w(Tag, "Failed - " + t.getMessage());
}
});
TestInterface
public interface TestInterface {
@GET("FetchCountries.php")
Call<List<CountriesModel>> getCountries();
}
CountriesModel Class
public class CountriesModel {
@SerializedName("countryId")
private long countryId;
@SerializedName("countryName")
private String countryName;
@SerializedName("image")
private String image;
public long getCountryId() {
return countryId;
}
public String getCountryName() {
return countryName;
}
public String getImage() {
return image;
}
}
The Columns In MySQL
The country name are shown in the chip like this {"ar": "ألمانيا", "default": "Germany"}, {"ar": "إيطاليا", "default": "Italy"}, {"ar": "هولندا", "default": "Netherlands"}, etc...
The JSON who inside the country name. Can I store it in HashMap and get the value depending on the key like below?
chip.setText(item.getCountryName().get("default"));
Can I do something like this using GsonConverterFactory
?
This answer is close to what I want, but I want to do that in the same model without creating two models.