Reputation: 391
Hai Iam new to retrofit and aim trying to get data from a url using retrofit library and here is my json data
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.
Upvotes: 2
Views: 770
Reputation: 2558
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) {
}
});
Upvotes: 1
Reputation: 3332
Your base url needs to end with "/" as specified by Retrofit documentation.
APIClient:
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;
}
}
APIInterface:
public interface ApiInterface {
@GET("country/get/all")
Call<RestResponse> getData();
}
Upvotes: 1
Reputation: 1086
/** * API CALLING */
private static ServiceInterface apiService;
public ServiceInterface callWSAds() {
if (!isInternetAvailable()) {
}
OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
builder.readTimeout(HTTP_TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(HTTP_TIMEOUT, TimeUnit.SECONDS)
.connectTimeout(HTTP_TIMEOUT, TimeUnit.SECONDS)
.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request originalRequest = chain.request();
Request.Builder requestBuilder = originalRequest.newBuilder().method(originalRequest.method(), originalRequest.body());
Request request = requestBuilder.build();
return chain.proceed(request);
}
});
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.addInterceptor(interceptor);
OkHttpClient client = builder.build();
Retrofit retrofit;
retrofit = new Retrofit.Builder()
.baseUrl(DEV_BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
apiService = retrofit.create(ServiceInterface.class);
return apiService;
}
long HTTP_TIMEOUT = 80;
callWSAds().getAdsSettingsNew(map).enqueue(new Callback<AdsSetting>() { //Need to change here
@Override
public void onResponse(Call<AdsSetting> call, Response<AdsSetting> response) {
if (response.body() != null) {
saveResponseOffline(response.body());
setting = getResponseOffline();
launchApp();//To track the launches of application
}
}
@Override
public void onFailure(Call<AdsSetting> call, Throwable t) {
if (setting == null) {
loadJSONFromAsset();
}
}
});
This Example contains POST method calling. Please use this kind of configuration to make API calling simpler.
Upvotes: 1