我看到其他3篇关于此的帖子,但解决方案并没有奏效。每个端点都返回status code 404
,并且在retrofit2中响应为null。这是我的改造实施:
RetrofitService:
//code setup for HTTP client for Retrofit goes here
......
......
public static void initRetrofit() {
builder =
new Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL)
.addConverterFactory(GsonConverterFactory.create(initGson()));
}
BuildConfig
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildConfigField "String", "BASE_URL", "\"http://MY_SERVICE.azurewebsites.net\""
}
debug {
versionNameSuffix ".staging"
buildConfigField "String", "BASE_URL", "\"http://MY_SERVICE.azurewebsites.net\""
}
}
终点:
public interface APIServices {
@POST("api/users/register.json")
Call<HttpResponse<Auth>> register(@Body RegisterData data);
}
我在这里做错了什么?其他SO主题建议从/
中移除/api
/api/users/register.json
,我做了,但响应没有变化。
答案 0 :(得分:0)
您没有为Retrofit
对象设置HTTP客户端。请尝试使用OkHttpClient
。
public static void initRetrofit() {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(Config.HTTP_CONNECT_TIMEOUT, TimeUnit.MILLISECONDS)
.readTimeout(Config.HTTP_READ_TIMEOUT, TimeUnit.MILLISECONDS)
.writeTimeout(Config.HTTP_READ_TIMEOUT, TimeUnit.MILLISECONDS)
.build();
builder =
new Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create(initGson()));
}
另外,请检查您的API地址 - 尝试使用某些服务(例如Postman):输入您的地址(来自您的代码:&#39; http://MY_SERVICE.azurewebsites.netapi/users/register.json&#39;)。
答案 1 :(得分:0)
Retrofit2基本网址应以尾部斜杠结尾。尝试添加它:
buildConfigField "String", "BASE_URL", "\"http://MY_SERVICE.azurewebsites.net/\""