gpt4 book ai didi

java - Android studio - 带Retrofit2的外汇汇率API,无法运行应用程序

转载 作者:行者123 更新时间:2023-12-02 09:56:52 25 4
gpt4 key购买 nike

所以我尝试在应用程序上显示一些货币兑换,但我什至无法运行它,我遇到了这个错误:

2019-05-01 14:34:57.260 6798-6798/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 6798
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.IllegalArgumentException: Unable to create converter for java.util.List<com.example.myapplication.Currency>
for method CurrencyExchangeService.getCurrency
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.IllegalArgumentException: Unable to create converter for java.util.List<com.example.myapplication.Currency>
for method CurrencyExchangeService.getCurrency
at retrofit2.Utils.methodError(Utils.java:52)
at retrofit2.HttpServiceMethod.createResponseConverter(HttpServiceMethod.java:70)
at retrofit2.HttpServiceMethod.parseAnnotations(HttpServiceMethod.java:46)
at retrofit2.ServiceMethod.parseAnnotations(ServiceMethod.java:36)
at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:168)
at retrofit2.Retrofit$1.invoke(Retrofit.java:147)
at java.lang.reflect.Proxy.invoke(Proxy.java:1006)
at $Proxy0.getCurrency(Unknown Source)
at com.example.myapplication.MainActivity.onCreate(MainActivity.java:33)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
... 11 more
Caused by: java.lang.IllegalArgumentException: class com.example.myapplication.Currency declares multiple JSON fields named CAD
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:170)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
at com.google.gson.Gson.getAdapter(Gson.java:423)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory.create(CollectionTypeAdapterFactory.java:53)
at com.google.gson.Gson.getAdapter(Gson.java:423)
at retrofit2.converter.gson.GsonConverterFactory.responseBodyConverter(GsonConverterFactory.java:64)
at retrofit2.Retrofit.nextResponseBodyConverter(Retrofit.java:328)
at retrofit2.Retrofit.responseBodyConverter(Retrofit.java:311)
at retrofit2.HttpServiceMethod.createResponseConverter(HttpServiceMethod.java:68)
... 22 more

Logcat 将我导航到这行代码

        Call<List<Currency>> call = currencyInterface.getCurrency();

我认为,这是在接口(interface)中调用方法,我错误地命名了 @Get 方法中的字符串。

这是代码

主要 Activity

public class MainActivity extends AppCompatActivity {
private TextView textViewResult;

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

textViewResult = findViewById(R.id.text_view_result);

Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.exchangeratesapi.io/")
.addConverterFactory(GsonConverterFactory.create())
.build();

CurrencyExchangeService currencyInterface = retrofit.create(CurrencyExchangeService.class);
Call<List<Currency>> call = currencyInterface.getCurrency();
call.enqueue(new Callback<List<Currency>>() {
@Override
public void onResponse(Call<List<Currency>> call, Response<List<Currency>> response) {
List<Currency> list = response.body();

for (Currency currency : list) {
String content = "";
content += "a: "+ currency.getEUR();
content += "b: "+ currency.getCAD();
content += "c: "+ currency.getGBP();

textViewResult.append(content);
}
}

@Override
public void onFailure(Call<List<Currency>> call, Throwable t) {
textViewResult.setText(t.getMessage());
}
});
}
}

货币

public class Currency {

private int CAD,CHF,GBP,SEK,EUR,USD;

@SerializedName("CAD")
private String cad;


public Currency(int CAD, int CHF, int GBP, int SEK, int EUR, int USD) {
this.CAD = CAD;
this.CHF = CHF;
this.GBP = GBP;
this.SEK = SEK;
this.EUR = EUR;
this.USD = USD;
}

public int getCAD() {
return CAD;
}

public int getCHF() {
return CHF;
}

public int getGBP() {
return GBP;
}

public int getSEK() {
return SEK;
}

public int getEUR() {
return EUR;
}

public int getUSD() {
return USD;
}
}

货币兑换接口(interface)

public interface CurrencyExchangeService {
@GET("latest?base=USD")
Call<List<Currency>> getCurrency();
}

问题出在哪里?我对界面中这个名字的理解正确吗?那么,如果我想进行最新的货币兑换,我应该使用哪个?我正在使用 Retrofit 2,并且 https://exchangeratesapi.io/ API。

最佳答案

正如网站上所述,您可能会收到此类 JSON 响应

{
"base": "EUR",
"date": "2018-04-08",
"rates": {
"CAD": 1.565,
"CHF": 1.1798,
"GBP": 0.87295,
"SEK": 10.2983,
"EUR": 1.092,
"USD": 1.2234,
...
}
}

因此,要解析这种类型的 JSON,您的 POJO 类应该如下所示

public class MyPojo
{
private String date;

private Rates rates;

private String base;

public String getDate ()
{
return date;
}

public void setDate (String date)
{
this.date = date;
}

public Rates getRates ()
{
return rates;
}

public void setRates (Rates rates)
{
this.rates = rates;
}

public String getBase ()
{
return base;
}

public void setBase (String base)
{
this.base = base;
}

@Override
public String toString()
{
return "ClassPojo [date = "+date+", rates = "+rates+", base = "+base+"]";
}

public class Rates
{
private String CHF;

private String EUR;

private String GBP;

private String CAD;

private String USD;

private String SEK;

public String getCHF ()
{
return CHF;
}

public void setCHF (String CHF)
{
this.CHF = CHF;
}

public String getEUR ()
{
return EUR;
}

public void setEUR (String EUR)
{
this.EUR = EUR;
}

public String getGBP ()
{
return GBP;
}

public void setGBP (String GBP)
{
this.GBP = GBP;
}

public String getCAD ()
{
return CAD;
}

public void setCAD (String CAD)
{
this.CAD = CAD;
}

public String getUSD ()
{
return USD;
}

public void setUSD (String USD)
{
this.USD = USD;
}

public String getSEK ()
{
return SEK;
}

public void setSEK (String SEK)
{
this.SEK = SEK;
}

@Override
public String toString()
{
return "ClassPojo [CHF = "+CHF+", EUR = "+EUR+", GBP = "+GBP+", CAD = "+CAD+", USD = "+USD+", SEK = "+SEK+"]";
}
}
}

Reference link

让我知道它是否有效

关于java - Android studio - 带Retrofit2的外汇汇率API,无法运行应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55936564/

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