Apurv
Apurv

Reputation: 49

Android - unable to fetch JSON using Retrofit2

I'm trying to get a JSON file using an URL, but the application is crashing.

JSON file api

MainActivity.java

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(ApiService.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

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

// Logs show error is in the code below
service.getPopulationData(new Callback<Flag> (){
        @Override
        public void onResponse(Call<Flag> call, Response<Flag> response) {
            Log.d("JSONData", response.body().toString());
        }

        @Override
        public void onFailure(Call<Flag> call, Throwable t) {
            Log.d("JSONData", t.getMessage());
        }
    });

ApiService.java

public interface ApiService {
    String BASE_URL = "http://www.androidbegin.com/";

    @GET("tutorial/jsonparsetutorial.txt")
    public void getPopulationData(Callback<Flag> callback) ;
}

Flag.java

public class Flag {
    private int rank;
    private String country;
    private String population;
    private String flag;

    public int getRank() {
        return rank;
    }

    public void setRank(int rank) {
        this.rank = rank;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getPopulation() {
        return population;
    }

    public void setPopulation(String population) {
        this.population = population;
    }

    public String getFlag() {
        return flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }
}

Edit: Error log can be found here: log

I've tried other solutions mentioned on stackoverflow, but I've been unable to get it right.

Also, I only want the flag URLs from the JSON file. How am I supposed to get it?

Upvotes: 1

Views: 86

Answers (2)

Navneet Krishna
Navneet Krishna

Reputation: 5017

You will need the following two pojo class

JsonResponse.java

    public class JsonResponse {

@SerializedName("worldpopulation")
@Expose
private List<Worldpopulation> worldpopulation = null;

public List<Worldpopulation> getWorldpopulation() {
return worldpopulation;
}

public void setWorldpopulation(List<Worldpopulation> worldpopulation) {
this.worldpopulation = worldpopulation;
}

}

Worldpopulation.java

    public class Worldpopulation {

@SerializedName("rank")
@Expose
private Integer rank;
@SerializedName("country")
@Expose
private String country;
@SerializedName("population")
@Expose
private String population;
@SerializedName("flag")
@Expose
private String flag;

public Integer getRank() {
return rank;
}

public void setRank(Integer rank) {
this.rank = rank;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

public String getPopulation() {
return population;
}

public void setPopulation(String population) {
this.population = population;
}

public String getFlag() {
return flag;
}

public void setFlag(String flag) {
this.flag = flag;
}

}

and make a retrofit call like below

service.getPopulationData(new Callback<JsonResponse> (){
    @Override
    public void onResponse(Call<JsonResponse> call, Response<JsonResponse> response) {
        Log.d("JSONData", response.body().toString());
        ArrayList<Worldpopulation> population=new ArrayList(response.body().getWorldpopulation());
    }

    @Override
    public void onFailure(Call<JsonResponse> call, Throwable t) {
        Log.d("JSONData", t.getMessage());
    }
});

**** edited as per requirement ****

and change ApiService.java

public interface ApiService {
String BASE_URL = "http://www.androidbegin.com/";

@GET("tutorial/jsonparsetutorial.txt")
Call<JsonResponse> getPopulationData() ;
}

and call it like this made an edit here

ApiService service = retrofit.create(ApiService.class);
Call<JsonResponse> call = service.getPopulationData();
call.enqueue(new Callback<JsonResponse> (){
@Override
public void onResponse(Call<JsonResponse> call, Response<JsonResponse> response) {
    Log.d("JSONData", response.body().toString());
    ArrayList<Worldpopulation> population=new ArrayList(response.body().getWorldpopulation());
}

@Override
public void onFailure(Call<JsonResponse> call, Throwable t) {
    Log.d("JSONData", t.getMessage());
}
});

Upvotes: 1

Karan Kalsi
Karan Kalsi

Reputation: 819

The json you are trying to parse with Retrofit contains a JSON Array as its root worldpopulation , So First you need a class WorldPopulation as follow:

public class WorldPopulation
{
private List<Flag> worldpopulation;

    public List<Flag> getWorldpopulation() {
        return worldpopulation;
    }

    public void setWorldpopulation(List<Flag> worldpopulation) {
        this.worldpopulation = worldpopulation;
    }
}


public interface ApiService {
    String BASE_URL = "http://www.androidbegin.com/";

    @GET("tutorial/jsonparsetutorial.txt")
    public void getPopulationData(Callback<WorldPopulation> callback) ;
}

Upvotes: 0

Related Questions