2

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.

Armando Sudi
  • 137
  • 1
  • 12

3 Answers3

3

If you are using emulator. check the network icon its connect with internet or not. its connected - this error raised in host address. otherwise, close the emulator and restart.

share network connected to emulator,

https://answers.microsoft.com/en-us/windows/forum/windows_10-networking-winpc/internet-connection-sharing-in-windows-10/f6dcac4b-5203-4c98-8cf2-dcac86d98fb9

Gayathri
  • 249
  • 3
  • 13
0

If you use VPN in your computer then emulator will not be able to connect to internet. Disconnect from VPN and try again.

Koustuv Ganguly
  • 897
  • 7
  • 21
0

Better test in physical device to avoid hassles. Still if you need emulator, Don't use / at last of base url, rather prepend it to suburl

Apurba A
  • 71
  • 6