所以,我在localhost上做我的项目,一切都运行得很顺利。我可以向服务器发出请求,并从我的安卓系统得到响应。没问题。然后我从AWS得到了一个ec2,没有Map到任何域名。所以,当我使用curl或postman发出请求时,它工作得很好。一切都工作得很好。但我试图从我的Retrofit发出请求时,它说的是同一个URL
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
我还确保我已经在我的手机和一切互联网许可。那么,它是一个必须有一个域名时,使网络请求从一个android手机?有没有办法绕过这样的事情?
这是我的改进的单例类,如果它有用的话。谢谢
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();
}
}
1条答案
按热度按时间wlzqhblo1#
通过在您的基本URL中添加一个斜杠来解决。也就是说,如果您的基本URL是www.some_base_url.com,则在启动一个改型示例时,它应该是www.some_base_url.com/。因此,如果出现该错误,请确保在最后添加一个斜杠。