2017-07-07 28 views
1

我試圖更新此RetroFit + Otto tutorial,所以我的代碼更新是:java.lang。 RuntimeException找不到改進註釋。 (參數#3)

IWeather.java

改造2 +不允許返回void,所以不是void getWeather(...)我加了Call<Weather> getWeather(...)

public interface IWeather { 

    @GET("/{latitude},{longitude}") 
    Call<Weather> getWeather(@Path("latitude") String latitude, 
          @Path("longitude") String longitude, 
          Callback<Weather> callback); 
} 

ForecastClient.java

改造2 +改變了他的構造函數,所以新ForecastClient有一個表單。 IllegalArgumentException指向weather.getWeather(...)方法。

public class ForecastClient { 

    private static final String BASE_URL = "https://api.darksky.net/forecast/"; 
    private static final String API_KEY = "******************"; 
    public static final String API_URL = BASE_URL + API_KEY + "/"; 

    private static ForecastClient mForecastClient; 
    private static Retrofit mRetroAdapter; 

    public static ForecastClient getClient() { 
     if (mForecastClient == null) { 
      mForecastClient = new ForecastClient(); 
     } 
     return mForecastClient; 
    } 

    private ForecastClient() { 
     mRetroAdapter = new Retrofit.Builder() 
       .baseUrl(API_URL) 
       .addConverterFactory(GsonConverterFactory.create()) 
       .client(new OkHttpClient()) 
       .build(); 
    } 

    public void getWeather(String latitude, String longitude, Callback<Weather> callback) { 
     IWeather weather = mRetroAdapter.create(IWeather.class); 
     weather.getWeather(latitude, longitude, callback); 
    } 
} 

ForecastManager.java

public class ForecastManager { 

    private Context mContext; 
    private Bus mBus; 
    private ForecastClient sForecastClient; 

    public ForecastManager(Context context, Bus bus) { 
     this.mContext = context; 
     this.mBus = bus; 
     sForecastClient = ForecastClient.getClient(); 
    } 

    @Subscribe 
    public void onGetWeatherEvent(GetWeatherEvent getWeatherEvent) { 
     String latitude = Double.toString(getWeatherEvent.getLatitude()).trim(); 
     String longitude = Double.toString(getWeatherEvent.getLongitude()).trim(); 

     Callback<Weather> callback = new Callback<Weather>() { 
      @Override 
      public void onResponse(Call<Weather> call, Response<Weather> response) { 
       Log.d(ForecastManager.class.getSimpleName(), response.body().toString()); 
       mBus.post(new SendWeatherEvent(response.body())); 
      } 

      @Override 
      public void onFailure(Call<Weather> call, Throwable t) { 
       Log.e(ForecastManager.class.getSimpleName(), t.getMessage()); 
      } 
     }; 

     sForecastClient.getWeather(latitude, longitude, callback); 
    } 
} 

我收到一個沒有改造註釋發現,我想原因是一些與我的回調有關,但它是一個改造的回調,所以我不不明白爲什麼錯誤。

堆棧跟蹤點在MainActivity,特別是向奧托的方法post()java.lang.RuntimeException: Could not dispatch event,造成的誤差前面提到java.lang.IllegalArgumentException: No Retrofit annotation found. (parameter #3)

MainActivity.java

public class MainActivity extends AppCompatActivity { 

    @BindView(R.id.activity_main_textView) 
    TextView textView; 
    @BindView(R.id.activity_main_button) 
    Button button; 

    private static final double LATITUDE = **.******; 
    private static final double LONGITUDE = **.******; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     ButterKnife.bind(this); 
    } 

    @OnClick(R.id.activity_main_button) 
    public void onClick(View view) { 
     BusProvider.getInstace().post(new GetWeatherEvent(LATITUDE, LONGITUDE)); 
    } 

    @Subscribe 
    public void onSendWeatherEvent(SendWeatherEvent sendWeatherEvent) { 
     Weather weather = sendWeatherEvent.getWeather(); 
     Currently currently = weather.getCurrently(); 
     textView.setText(currently.getSummary()); 
    } 

約其中任何線索是錯誤還是應該如何處理總線訂閱,回調等等?

回答

3

java.lang.IllegalArgumentException: No Retrofit annotation found. (parameter #3)

由於錯誤說,該問題是getWeather方法的第三個參數沒有註釋。 Callback類用於Call#enqueue(Callback)方法。

只需將sForecastClient.getWeather(latitude, longitude, callback)更改爲sForecastClient.getWeather(latitude, longitude).enqueue(callback)並刪除第三個參數。

+0

它就像一個魅力。我很新使用Otto和RetroFit,所有的教程都使用這些庫的舊版本。 –

+0

Square的大多數圖書館在他們的項目頁面上都有很好的例子,在使用庫時應該在教程之前引用它們。 – Kiskae