我正在尝试在我的应用程序中实现OpenWeatherMap API,并且我在执行时遇到错误。
我的代码:
public class WeatherProvider {
private static WeatherService mWeatherService = null;
public interface WeatherService {
@GET("APPID=" + API_KEY)
Call<GetWeatherResponse> getWeather(@Query("lat") String latitude, @Query("lon") String longitude);
}
public static WeatherService getWeatherAPI(){
if (mWeatherService == null){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://api.openweathermap.org/data/2.5/weather?")
.addConverterFactory(GsonConverterFactory.create())
.build();
mWeatherService = retrofit.create(WeatherService.class);
}
return mWeatherService;
}}
对于示例URI调用:http://api.openweathermap.org/data/2.5/weather?lat=12.9176315&lon=77.5909999&APPID= {API + KEY}
当我尝试使用此代码调用WeatherAPI时
Call<GetWeatherResponse> call = WeatherProvider.getWeatherAPI()
.getWeather(String.valueOf(mLocationFetcher.fetchLatitude()),String.valueOf(mLocationFetcher.fetchLongitude()));
call.enqueue(new Callback<GetWeatherResponse>() {
@Override
public void onResponse(Call<GetWeatherResponse> call, Response<GetWeatherResponse> response) {
Log.d(LOG_TAG, String.valueOf(response));
}
@Override
public void onFailure(Call<GetWeatherResponse> call, Throwable t) {
}
});
我收到以下错误:
FATAL EXCEPTION: main
Process: com.simplicity.anuj.myday, PID: 28218
java.lang.IllegalArgumentException: Unable to create call adapter for retrofit2.Call<com.simplicity.anuj.myday.Weather.GetWeatherResponse>
for method WeatherService.getWeather
at retrofit.Utils.methodError(Utils.java:177)
at retrofit.MethodHandler.createCallAdapter(MethodHandler.java:47)
at retrofit.MethodHandler.create(MethodHandler.java:26)
at retrofit.Retrofit.loadMethodHandler(Retrofit.java:151)
at retrofit.Retrofit$1.invoke(Retrofit.java:132)
at java.lang.reflect.Proxy.invoke(Proxy.java:813)
at $Proxy1.getWeather(Unknown Source)
at com.simplicity.anuj.myday.Activity.AddEntryActivity$3.onClick(AddEntryActivity.java:138)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.IllegalArgumentException: Could not locate call adapter for retrofit2.Call<com.simplicity.anuj.myday.Weather.GetWeatherResponse>. Tried:
* retrofit.ExecutorCallAdapterFactory
at retrofit.Retrofit.nextCallAdapter(Retrofit.java:207)
at retrofit.Retrofit.callAdapter(Retrofit.java:175)
at retrofit.MethodHandler.createCallAdapter(MethodHandler.java:45)
... 15 more
在我通过Callback调用API的部分。我哪里错了。请帮忙。 POJO类是使用jsonschema2pojo创建的。