Ashutosh Soni
Ashutosh Soni

Reputation: 1025

java.net cannot resolve Domain name

So, I was doing my project on localhost and everything ran smooth. I was able to make request call to the server and getting the response from my android. No problem. Then I got a ec2 from AWS and did't map to any domain name. SO, When I use curl or postman to make the request it works totally fine. Everything works great. But the same URL I try to make a request from my Retrofit it says

HTTP FAILED: java.net.UnknownHostException: Unable to resolve host "ec2-11-130-81-251.ap-south-1.compute.amazonaws.comapi": No address associated with hostname

I also made sure that I have given internet permission in my phone and everything. So, is it a must to have a domain name when making network request from an android phone? is there any way to bypass such things?

here is my retrofit singleton class if it finds useful. Thanks

public class ApiClient {

//    public static final String BASE_LOCAL_URL = "http://192.168.1.4:3000/";
//    public static final String BASE_LOCAL_EMULATOR_URL = "http://10.0.2.2:3000/";
        public static final String SERVIER_LOGIN="ec2-11-130-81-251.ap-south-1.compute.amazonaws.comapi";
    public static final String BASE_URL = SERVIER_LOGIN;

    private static Retrofit retrofit = null;

    private ApiClient() {


        if (retrofit != null) {
            throw new RuntimeException("Use getInstance() method to get the single instance of this class.");
        }
    }

    public static synchronized Retrofit getClient(Context context) {


        if (retrofit == null) {
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.level(HttpLoggingInterceptor.Level.BODY);


            AuthInterceptor authInterceptor = new AuthInterceptor(context);
            OkHttpClient client = new OkHttpClient.Builder().
                    addInterceptor(authInterceptor).
                    readTimeout(60, TimeUnit.SECONDS)
                    .writeTimeout(60, TimeUnit.SECONDS)
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .addInterceptor(interceptor)
                    .build();


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

            return retrofit;
        }


        return retrofit;
    }

    private static Gson getGson() {


        return new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").create();
    }
}

Upvotes: 0

Views: 860

Answers (1)

Ashutosh Soni
Ashutosh Soni

Reputation: 1025

Resolved by adding a slash in your base URL. That is if your base URL is www.some_base_url.com it should be www.some_base_url.com/ when initiating a retrofit instance. So. Make sure you're adding a slash in the end if it gives that error.

Upvotes: 1

Related Questions