Ósc*_*car 2 java android otto retrofit
我正在尝试更新这个RetroFit + Otto教程,所以我的代码更新是:
IWeather.java
RetroFit 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);
}
Run Code Online (Sandbox Code Playgroud)
ForecastClient.java
RetroFit 2. +已经改变了他的构造函数,所以new 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);
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我收到了一个No Retrofit注释,我猜原因是与我的回调有关,但它是一个RetroFit回调,所以我不明白为什么错误.
stacktrace指向MainActivity,特别是对于post()带有a 的Otto方法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());
}
Run Code Online (Sandbox Code Playgroud)
关于错误在哪里或者我应该如何处理总线订阅,回调等等的任何线索?
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)并删除第三个参数.
小智 7
尝试一下并将您的改造版本号修改为2.6.0. 协程中的使用Retrofit 2.6.0可以Response直接返回一个对象。如果await()不需要,Retrofit 会自动调用它。当版本低于 时,这不适用2.6.0。