所以,我在本地主机上进行项目,一切运行顺利。我能够对服务器进行请求调用,并从我的android得到响应。没问题。然后我从AWS得到了一个ec2,并且没有映射到任何域名。所以,当我使用curl或邮递员发出请求时,它可以很好地工作。一切正常。但是我尝试从我的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();
}
}
通过在基本URL中添加斜杠来解决。就是说,如果您的基本URL是www.some_base_url.com,则在启动改造实例时,它应该是www.some_base_url.com/。所以。如果出现该错误,请确保最后添加一个斜杠。