Android Retrofit Localhost Connect Error) Failed to connect to localhost/127.0.0.1:5000

 

문제

Retrofit 에서 로컬호스트의 주소로 테스트를 진행하려니 에러 발생


해결

  • 로컬호스트 주소를 아래의 코드로 변경
    • 10.0.0.2

 

  • 예시)
public static Retrofit retrofit;

public static Retrofit getRetrofitClient(Context context){
    if(retrofit == null){
        // 네트워크 통신 로그
        HttpLoggingInterceptor loggingInterceptor =
                new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
        // 네트워크 연결
        OkHttpClient httpClient = new OkHttpClient.Builder()
                .connectTimeout(1, TimeUnit.MINUTES)
                .readTimeout(1, TimeUnit.MINUTES)
                .writeTimeout(1, TimeUnit.MINUTES)
                .addInterceptor(loggingInterceptor)
                .build();

        // Localhost 입력
        retrofit = new Retrofit.Builder().baseUrl(10.0.0.2)
                .client(httpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
}