Android App with Retrofit consuming of api does not show the list

0

Well, the fact is that I just want to show the name of the countries in a list, but I do not know how to get the name only:

This is the main class:

 listView = (ListView) findViewById(R.id.list_view);
        textView = (TextView) findViewById(R.id.text_view);


        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(ICountryService.ENDPOINT)
                .addConverterFactory(GsonConverterFactory.create(new GsonBuilder().create()))
                .build();

        ICountryService service = retrofit.create(ICountryService.class);

        Call<List<Country>> call = service.getCountry();


        call.enqueue(new Callback<List<Country>>() {
            @Override
            public void onResponse(Response<List<Country>> response, Retrofit retrofit) {
                if (response.isSuccess()) {
                    List<Country> countryList = response.body();
                    ArrayAdapter<Country> adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, countryList);
                    listView.setAdapter(adapter);
                } else {
                    Toast.makeText(MainActivity.this, "Error: " + response.code(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Throwable t) {
                Toast.makeText(MainActivity.this, "Error: " + t.getMessage(), Toast.LENGTH_SHORT).show();
            }

        });
    }

The interface:

public interface ICountryService {

    String ENDPOINT = "https://restcountries.eu";


    @GET("/rest/v1/all")
    Call<List<Country>> getCountry();

}

The Country class is very extensive but of the whole class I just want to show the list of countries. How do I do it?:

public class Country {

    /**
     * 
     * (Required)
     * 
     */
    @SerializedName("name")
    @Expose
    private String name;

Of course, this class has gotters and setters generated

OnResponse method (I leave here this implementation already given in the solution but more adapted to the exercise):

 call.enqueue(new Callback<List<Country>>() {
            @Override
            public void onResponse(Response<List<Country>> response, Retrofit retrofit) {
                if (response.isSuccess()) {
                    List<Country> countryList = response.body();
                    List<String> listaNombres = new ArrayList<String>();

                    for (Country c : countryList
                            ) {
                        listaNombres.add(c.getName());


                        ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, listaNombres);
                        listView.setAdapter(adapter);
                    }
                } else {
                    Toast.makeText(MainActivity.this, "Error: " + response.code(), Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Throwable t) {
                Toast.makeText(MainActivity.this, "Error: " + t.getMessage(), Toast.LENGTH_LONG).show();
            }

        });
    
asked by Jose 27.11.2016 в 13:22
source

1 answer

1

Using the information obtained from link and using the page link to get the next class

-----------------------------------com.example.Country.java-----------------------------------

package com.example;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("org.jsonschema2pojo")
public class Country {

@SerializedName("name")
@Expose
private String name;
@SerializedName("topLevelDomain")
@Expose
private List<String> topLevelDomain = new ArrayList<String>();
@SerializedName("alpha2Code")
@Expose
private String alpha2Code;
@SerializedName("alpha3Code")
@Expose
private String alpha3Code;
@SerializedName("callingCodes")
@Expose
private List<String> callingCodes = new ArrayList<String>();
@SerializedName("capital")
@Expose
private String capital;
@SerializedName("altSpellings")
@Expose
private List<String> altSpellings = new ArrayList<String>();
@SerializedName("relevance")
@Expose
private String relevance;
@SerializedName("region")
@Expose
private String region;
@SerializedName("subregion")
@Expose
private String subregion;
@SerializedName("translations")
@Expose
private Translations translations;
@SerializedName("population")
@Expose
private Integer population;
@SerializedName("latlng")
@Expose
private List<Double> latlng = new ArrayList<Double>();
@SerializedName("demonym")
@Expose
private String demonym;
@SerializedName("area")
@Expose
private Double area;
@SerializedName("gini")
@Expose
private Object gini;
@SerializedName("timezones")
@Expose
private List<String> timezones = new ArrayList<String>();
@SerializedName("borders")
@Expose
private List<Object> borders = new ArrayList<Object>();
@SerializedName("nativeName")
@Expose
private String nativeName;
@SerializedName("numericCode")
@Expose
private String numericCode;
@SerializedName("currencies")
@Expose
private List<String> currencies = new ArrayList<String>();
@SerializedName("languages")
@Expose
private List<String> languages = new ArrayList<String>();

/**
* 
* @return
* The name
*/
public String getName() {
return name;
}

/**
* 
* @param name
* The name
*/
public void setName(String name) {
this.name = name;
}

/**
* 
* @return
* The topLevelDomain
*/
public List<String> getTopLevelDomain() {
return topLevelDomain;
}

/**
* 
* @param topLevelDomain
* The topLevelDomain
*/
public void setTopLevelDomain(List<String> topLevelDomain) {
this.topLevelDomain = topLevelDomain;
}

/**
* 
* @return
* The alpha2Code
*/
public String getAlpha2Code() {
return alpha2Code;
}

/**
* 
* @param alpha2Code
* The alpha2Code
*/
public void setAlpha2Code(String alpha2Code) {
this.alpha2Code = alpha2Code;
}

/**
* 
* @return
* The alpha3Code
*/
public String getAlpha3Code() {
return alpha3Code;
}

/**
* 
* @param alpha3Code
* The alpha3Code
*/
public void setAlpha3Code(String alpha3Code) {
this.alpha3Code = alpha3Code;
}

/**
* 
* @return
* The callingCodes
*/
public List<String> getCallingCodes() {
return callingCodes;
}

/**
* 
* @param callingCodes
* The callingCodes
*/
public void setCallingCodes(List<String> callingCodes) {
this.callingCodes = callingCodes;
}

/**
* 
* @return
* The capital
*/
public String getCapital() {
return capital;
}

/**
* 
* @param capital
* The capital
*/
public void setCapital(String capital) {
this.capital = capital;
}

/**
* 
* @return
* The altSpellings
*/
public List<String> getAltSpellings() {
return altSpellings;
}

/**
* 
* @param altSpellings
* The altSpellings
*/
public void setAltSpellings(List<String> altSpellings) {
this.altSpellings = altSpellings;
}

/**
* 
* @return
* The relevance
*/
public String getRelevance() {
return relevance;
}

/**
* 
* @param relevance
* The relevance
*/
public void setRelevance(String relevance) {
this.relevance = relevance;
}

/**
* 
* @return
* The region
*/
public String getRegion() {
return region;
}

/**
* 
* @param region
* The region
*/
public void setRegion(String region) {
this.region = region;
}

/**
* 
* @return
* The subregion
*/
public String getSubregion() {
return subregion;
}

/**
* 
* @param subregion
* The subregion
*/
public void setSubregion(String subregion) {
this.subregion = subregion;
}

/**
* 
* @return
* The translations
*/
public Translations getTranslations() {
return translations;
}

/**
* 
* @param translations
* The translations
*/
public void setTranslations(Translations translations) {
this.translations = translations;
}

/**
* 
* @return
* The population
*/
public Integer getPopulation() {
return population;
}

/**
* 
* @param population
* The population
*/
public void setPopulation(Integer population) {
this.population = population;
}

/**
* 
* @return
* The latlng
*/
public List<Double> getLatlng() {
return latlng;
}

/**
* 
* @param latlng
* The latlng
*/
public void setLatlng(List<Double> latlng) {
this.latlng = latlng;
}

/**
* 
* @return
* The demonym
*/
public String getDemonym() {
return demonym;
}

/**
* 
* @param demonym
* The demonym
*/
public void setDemonym(String demonym) {
this.demonym = demonym;
}

/**
* 
* @return
* The area
*/
public Double getArea() {
return area;
}

/**
* 
* @param area
* The area
*/
public void setArea(Double area) {
this.area = area;
}

/**
* 
* @return
* The gini
*/
public Object getGini() {
return gini;
}

/**
* 
* @param gini
* The gini
*/
public void setGini(Object gini) {
this.gini = gini;
}

/**
* 
* @return
* The timezones
*/
public List<String> getTimezones() {
return timezones;
}

/**
* 
* @param timezones
* The timezones
*/
public void setTimezones(List<String> timezones) {
this.timezones = timezones;
}

/**
* 
* @return
* The borders
*/
public List<Object> getBorders() {
return borders;
}

/**
* 
* @param borders
* The borders
*/
public void setBorders(List<Object> borders) {
this.borders = borders;
}

/**
* 
* @return
* The nativeName
*/
public String getNativeName() {
return nativeName;
}

/**
* 
* @param nativeName
* The nativeName
*/
public void setNativeName(String nativeName) {
this.nativeName = nativeName;
}

/**
* 
* @return
* The numericCode
*/
public String getNumericCode() {
return numericCode;
}

/**
* 
* @param numericCode
* The numericCode
*/
public void setNumericCode(String numericCode) {
this.numericCode = numericCode;
}

/**
* 
* @return
* The currencies
*/
public List<String> getCurrencies() {
return currencies;
}

/**
* 
* @param currencies
* The currencies
*/
public void setCurrencies(List<String> currencies) {
this.currencies = currencies;
}

/**
* 
* @return
* The languages
*/
public List<String> getLanguages() {
return languages;
}

/**
* 
* @param languages
* The languages
*/
public void setLanguages(List<String> languages) {
this.languages = languages;
}

}
-----------------------------------com.example.Translations.java-----------------------------------

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("org.jsonschema2pojo")
public class Translations {

@SerializedName("de")
@Expose
private String de;
@SerializedName("es")
@Expose
private String es;
@SerializedName("fr")
@Expose
private String fr;
@SerializedName("ja")
@Expose
private String ja;
@SerializedName("it")
@Expose
private String it;

/**
* 
* @return
* The de
*/
public String getDe() {
return de;
}

/**
* 
* @param de
* The de
*/
public void setDe(String de) {
this.de = de;
}

/**
* 
* @return
* The es
*/
public String getEs() {
return es;
}

/**
* 
* @param es
* The es
*/
public void setEs(String es) {
this.es = es;
}

/**
* 
* @return
* The fr
*/
public String getFr() {
return fr;
}

/**
* 
* @param fr
* The fr
*/
public void setFr(String fr) {
this.fr = fr;
}

/**
* 
* @return
* The ja
*/
public String getJa() {
return ja;
}

/**
* 
* @param ja
* The ja
*/
public void setJa(String ja) {
this.ja = ja;
}

/**
* 
* @return
* The it
*/
public String getIt() {
return it;
}

/**
* 
* @param it
* The it
*/
public void setIt(String it) {
this.it = it;
}

}

In the main view:

List<Country> countryList = response.body();
List<String> nameList;
foreach(Country country: countryList){
nameList.add(country.getName());
}
    
answered by 27.11.2016 / 17:57
source