java.lang.IllegalArgumentException:不能有replace块对于动态查询参数,请使用@Query

问题描述

我正在尝试使用具有'convert'端点的API将USD转换为INR b,这是向下的错误,在该错误下我会反复遇到

java.lang.IllegalArgumentException: URL query string "api_key={api_key}&base={base}&to={to}&amount={amount}" must not have replace block. For dynamic query parameters use @Query.
    for method RetrofitClient.getConvertedValue
    at retrofit2.Utils.methodError(Utils.java:52)
    at retrofit2.Utils.methodError(Utils.java:42)
    at retrofit2.RequestFactory$Builder.parseHttpMethodAndpath(RequestFactory.java:257)
    at retrofit2.RequestFactory$Builder.parseMethodAnnotation(RequestFactory.java:205)
    at retrofit2.RequestFactory$Builder.build(RequestFactory.java:161)
    at retrofit2.RequestFactory.parseAnnotations(RequestFactory.java:65)
    at retrofit2.ServiceMethod.parseAnnotations(ServiceMethod.java:25)
    at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:168)
    at retrofit2.Retrofit$1.invoke(Retrofit.java:147)
    at java.lang.reflect.Proxy.invoke(Proxy.java:813)
    at $Proxy0.getConvertedValue(UnkNown Source)
    at com.example.online_currency.MainActivity.onClick(MainActivity.java:57)
    at android.view.View.performClick(View.java:5609)
    at android.view.View$PerformClick.run(View.java:22263)
    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:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

//这是我在其中创建改造对象的班级

公共类MainActivity扩展了AppCompatActivity实现的View.OnClickListener {

private static final String TAG = "MainActivity";
private TextView result;
private EditText currency;
private Button button;
private static String BASE_URL = "https://api.currencyscoop.com/v1/";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.d(TAG,"onCreate: called");
    initViews();
    button.setonClickListener(this);
}

public void initViews(){
    Log.d(TAG,"initViews: called");
    result = findViewById(R.id.result);
    currency = findViewById(R.id.amount);
    button = findViewById(R.id.button);
}

@Override
public void onClick(View v) {
    Log.d(TAG,"onClick: called");
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    RetrofitClient retrofitClient = retrofit.create(RetrofitClient.class);
    Call<ConvertAmt> calling = retrofitClient.getConvertedValue("<my_api_key>","USD","INR",currency.getText().toString());
    calling.enqueue(new Callback<ConvertAmt>() {
        @Override
        public void onResponse(Call<ConvertAmt> call,Response<ConvertAmt> response) {
            Log.d(TAG,"onResponse: called :----------------------> "+response.body().getResult());
        }

        @Override
        public void onFailure(Call<ConvertAmt> call,Throwable t) {
            Toast.makeText(MainActivity.this,"Error "+t.getMessage(),Toast.LENGTH_LONG).show();
        }
    });
}

}

//here is my RetrofitClient interface

公共接口RetrofitClient {

@GET("convert?api_key={api_key}&base={base}&to={to}&amount={amount}")
Call<ConvertAmt> getConvertedValue(@Query("api_key") String api_key,@Query("base") String base,@Query("to") String to,@Query("amount") String amount);

}

解决方法

请尝试此操作,端点未正确提及。

@GET("/convert")
Call<ConvertAmt> getConvertedValue(@Query("api_key") String api_key,@Query("base") String base,@Query("to") String to,@Query("amount") String amount);