Search code examples
androidgsonretrofitpojo

Unable to get data using retrofit library


Hai Iam new to retrofit and aim trying to get data from a url using retrofit library and here is my json data

enter image description here

I would like to display the name in every array.

and here are the gson converted Pojo classes

RestResponse.java

public class RestResponse {
    @SerializedName("messages")
    @Expose
    private List<String> messages = null;
    @SerializedName("result")
    @Expose
    private List<Result> result = null;

    public List<String> getMessages() {
        return messages;
    }

    public void setMessages(List<String> messages) {
        this.messages = messages;
    }

    public List<Result> getResult() {
        return result;
    }

    public void setResult(List<Result> result) {
        this.result = result;
    }
}

and the Second one Result.java

public class Result {
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("alpha2_code")
    @Expose
    private String alpha2Code;
    @SerializedName("alpha3_code")
    @Expose
    private String alpha3Code;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAlpha2Code() {
        return alpha2Code;
    }

    public void setAlpha2Code(String alpha2Code) {
        this.alpha2Code = alpha2Code;
    }

    public String getAlpha3Code() {
        return alpha3Code;
    }

    public void setAlpha3Code(String alpha3Code) {
        this.alpha3Code = alpha3Code;
    }

}

and finally RestResponseMain.java // Example.java file generated by Gson Converter

public class RestResponseMain {

    @SerializedName("RestResponse")
    @Expose
    private RestResponse restResponse;

    public RestResponse getRestResponse() {
        return restResponse;
    }

    public void setRestResponse(RestResponse restResponse) {
        this.restResponse = restResponse;
    }
}

By using above classes i was trying to retrieve data by

The Actual URL is: http://services.groupkt.com/country/get/all

and my Apiclenit file is:

public class ApiClient {
    public static final String BaseUrl="http://services.groupkt.com";
    public static Retrofit retrofit = null;
    public static Retrofit getData()
    {
        if(retrofit==null)
        {
            retrofit = new Retrofit.Builder().baseUrl(BaseUrl).addConverterFactory(GsonConverterFactory.create()).build();
        }



        return retrofit;
    }
}

and ApiInterface is

public interface ApiInterface {
    @GET("/country/get/all")
    Call<RestResponse> getData();
}

and in MainActivity the action i have use is

ApiInterface apiInterface = ApiClient.getData().create(ApiInterface.class);
            Call<RestResponse> call = apiInterface.getData();
            pDialog.show();
            call.enqueue(new Callback<RestResponse>() {
                @Override
                public void onResponse(Call<RestResponse> call, Response<RestResponse> response) {
                    pDialog.dismiss();
                    List<Result> list = response.body().getResult();
                    MyRecyclerRetrofitAdapter adapter = new MyRecyclerRetrofitAdapter(MainActivity.this,list);
                    rView.setAdapter(adapter);
                }

                @Override
                public void onFailure(Call<RestResponse> call, Throwable t) {

                }
            });

and in adapter file i was using

holder.tvName.setText(data.get(position).getName());//using recycler & card view

Finally i didnt get any response from above process and iam new to retrofit concept and iam trying this by some online tutorials.

Suggest me the possible changes as per the pojo classes in above code.


Solution

  • you bind a wrong model calss so you just need to change and also Your base url needs to end with "/"

    RestResponse

    To

    RestResponseMain

    API client

    public class ApiClient {
        public final String baseUrl="http://services.groupkt.com/";
        public static Retrofit retrofit;
    
        public static Retrofit getData() {
    
            if (retrofit == null) {
                retrofit = new Retrofit.Builder()
                                .baseUrl(BaseUrl)
                                .addConverterFactory(GsonConverterFactory.create())
                                .build();
            }
            return retrofit;
        }
    }
    

    in API Interface and MainActivty.show below

    API interface

    public interface ApiInterface {
    @GET("country/get/all")
    Call<RestResponseMain> getData();
    }
    

    MainActivity

    ApiInterface apiInterface = ApiClient.getData().create(ApiInterface.class);
                Call<RestResponseMain> call = apiInterface.getData();
                pDialog.show();
                call.enqueue(new Callback<RestResponseMain>() {
                    @Override
                    public void onResponse(Call<RestResponseMain> call, Response<RestResponseMain> response) {
                        pDialog.dismiss();
                        List<Result> list = response.body().getResult();
                        MyRecyclerRetrofitAdapter adapter = new MyRecyclerRetrofitAdapter(MainActivity.this,list);
                        rView.setAdapter(adapter);
                    }
    
                    @Override
                    public void onFailure(Call<RestResponseMain> call, Throwable t) {
    
                    }
                });