我使用牛津词典API(https://developer.oxforddictionaries.com/)
并使用改进2来解析JSON
但是HTTP响应是200,但我得到空响应主体
public class WeatherApiRequest {
public static void getWeatherForecast(String language ,String word, Callback<WeatherForecast> callback) {
Retrofit retrofit = RetrofitBuilder.getRetrofit();
WeatherAPIs weatherAPIs = retrofit.create(WeatherAPIs.class);
Call<WeatherForecast> weatherForecast = weatherAPIs.getWeatherForecast(language ,word );
weatherForecast.enqueue(callback);
}
我的界面是:
public interface WeatherAPIs {
@Headers({
"Accept: application/json",
"app_id: {my-app_id}",
"app_key: {my-app-key}"
})
@GET("entries/{source_lang}/{word_id}")
Call<WeatherForecast> getWeatherForecast(@Path("source_lang") String language, @Path("word_id") String word);
}
我制作这个模型:
public class WeatherForecast {
@SerializedName("provider")
@Expose
private String provider;
public String getProvider() {
return provider;
}
public void setProvider(String provider) {
this.provider = provider;
}
}
并在主要活动中执行此操作:
@OnClick(R2.id.btnSearch)
public void onSearchClicked(View view) {
WeatherApiRequest.getWeatherForecast("en", "hello", new Callback<WeatherForecast>() {
@Override
public void onResponse(Call<WeatherForecast> call, Response<WeatherForecast> response) {
if (response.isSuccessful()) {
WeatherForecast weatherForecast = response.body();
StringBuilder builder = new StringBuilder();
builder.append(weatherForecast.getProvider() + "\n");
Toast.makeText(MainActivity.this, "response code : " + response.code() + " Provider = " + builder, Toast.LENGTH_SHORT).show();
//
Timber.i(builder.toString());
} else {
Toast.makeText(MainActivity.this, "Try later ! " + response.code(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<WeatherForecast> call, Throwable t) {
Toast.makeText(MainActivity.this, "Internet might not have been initialized !", Toast.LENGTH_SHORT).show();
}
});