gpt4 book ai didi

java.lang. RuntimeException 未找到 Retrofit 注释。 (参数#3)

转载 作者:太空狗 更新时间:2023-10-29 16:11:51 25 4
gpt4 key购买 nike

我正在尝试更新此 RetroFit + Otto tutorial ,所以我更新的代码是:

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);
}

ForecastClient.java

RetroFit 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);
}
}

我收到一个 No Retrofit annotation found,我猜原因与我的回调有关,但它是一个 RetroFit 回调,所以我不明白为什么会出现错误。

堆栈跟踪指向 MainActivity,特别是 Otto 的方法 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());
}

关于错误在哪里或我应该如何处理总线订阅、回调等的任何线索?

最佳答案

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)

并删除第三个参数。

关于java.lang. RuntimeException 未找到 Retrofit 注释。 (参数#3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44966286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com