I was following a tutorial on this link http://thetechnocafe.com/getting-started-with-retrofit-in-android/ to refresh my knowledge on how to use retrofit. But when I get the response, it calls onFailure with the following error Unable to resolve host "jsonplaceholder.typicode.com": No address associated with hostname. I have cheked the implementation and it's correct, I have even checked it with postman and the API is returning the correct expected JSON. The service is setup as follow:
package cd.acgt.acgtexp.service;
import java.util.concurrent.Executors;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class NetworkService {
private static NetworkService mInstance;
private static final String BASE_URL = "https://jsonplaceholder.typicode.com/";
private Retrofit mRetrofit;
private NetworkService() {
mRetrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.callbackExecutor(Executors.newSingleThreadExecutor())
.addConverterFactory(GsonConverterFactory.create())
.build();
}
public static NetworkService getInstance() {
if (mInstance == null) {
mInstance = new NetworkService();
}
return mInstance;
}
public JSONPlaceHolderApi getJSONApi() {
return mRetrofit.create(JSONPlaceHolderApi.class);
}
}
My API interface is implemented as follow :
public interface JSONPlaceHolderApi {
@GET("posts/{id}")
public Call<Post> getPostWithID(@Path("id") int id);
}
And here is where I call my API :
public void testApi() {
Toast.makeText(this, "click", Toast.LENGTH_SHORT).show();
Log.e(TAG, "test starts" );
NetworkService service = NetworkService.getInstance();
JSONPlaceHolderApi apiInterface = service.getJSONApi();
apiInterface.getPostWithID(1).enqueue(new Callback<Post>() {
@Override
public void onResponse(Call<Post> call, Response<Post> response) {
Log.e(TAG, "DONE");
}
@Override
public void onFailure(Call<Post> call, Throwable t) {
Log.e(TAG, "onFailure : " + t.getMessage());
Log.e(TAG, "onFailure : " + t.toString());
}
});
}
It's going only in the onfailed method and throwing the error that there is no address associated with the hostname.