gpt4 book ai didi

android - java.lang.IllegalArgumentException : must not have replace block. 对于动态查询参数使用@Query

转载 作者:行者123 更新时间:2023-12-04 23:59:36 25 4
gpt4 key购买 nike

我正在尝试使用带有“转换”端点的 API 将美元转换为 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)
//这是我创建改造对象的类
公共(public)类 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
公共(public)接口(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);

关于android - java.lang.IllegalArgumentException : must not have replace block. 对于动态查询参数使用@Query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63621285/

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