【发布时间】:2018-08-07 14:48:44
【问题描述】:
我正在使用 retrofit 和 openweather API 制作天气应用程序。
这个Link 引导我调用 API,但我总是遇到 retrofit2.BuiltInConverters$RequestBodyConverter 错误。
我试过这个solution,但不起作用。
我的 API 在这里:
http://api.openweathermap.org/data/2.5/weather?q=Ankara&APPID=6a5ea99c096c7ed68e620675d3eb2e2b
这是我的毕业生:
implementation 'com.google.code.gson:gson:2.8.2'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'io.reactivex.rxjava2:rxjava:2.2.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
这是 Weather.java:
public class Weather {
public int id;
public String main;
public String description;
public String icon;
public Weather(int id, String main, String description, String icon) {
this.id = id;
this.main = main;
this.description = description;
this.icon = icon;
}
public int getId() {
return id;
}
public String getMain() {
return main;
}
public String getDescription() {
return description;
}
public String getIcon() {
return icon;
}
}
这是 ApiClient.java:
public class ApiClient {
public static final String BASE_URL = WeatherService.BASE_URL;
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
这是我的界面:
public interface WeatherService {
//API LINK
String BASE_URL = "http://api.openweathermap.org/data/2.5";
String API_KEY = "6a5ea99c096c7ed68e620675d3eb2e2b";
//API SUBCLASS
@GET("/weather?")
//q=CityName &appid =APIKEY
Call<Weather> getWeatherData(@Query("q") String cityName,@Query("appid") String apikey);
}
最后是 MainActiviy.java:
public TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
//asyncTask.execute(API_KEY);
WeatherService weatherService = ApiClient.getClient().create(WeatherService.class);
Call<Weather> call = weatherService.getWeatherData("Ankara",weatherService.API_KEY);
call.enqueue(new Callback<Weather>() {
@Override
public void onResponse(Call<Weather> call, Response<Weather> response) {
Log.d("onResponse","I am here");
tv.setText(response.body().getDescription());
}
@Override
public void onFailure(Call<Weather> call, Throwable t) {
}
});
}
有什么问题,我该如何解决? 衷心感谢大家
【问题讨论】:
标签: android retrofit retrofit2 openweathermap