当前位置: 首页 > 知识库问答 >
问题:

Android Studio-Reverfit2的外汇汇率API,无法运行应用程序[重复]

习高格
2023-03-14
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();

即在接口中调用方法,我假设我在@Get方法中错误地命名了字符串

这是密码

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;
}
}
public interface CurrencyExchangeService {
@GET("latest?base=USD")
Call<List<Currency>> getCurrency();
}

问题出在哪里?我说的接口中的名字对吗?如果我想兑换最新的货币,我应该用哪一种?我使用的是reverfit2和https://exchangeratesapi.io/api。

共有1个答案

司空叶五
2023-03-14

正如网站上所述,您可能会得到这种类型的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+"]";
    }
}
}

引用链接

 类似资料:
  • 我正试图修复这个错误...但似乎什么都不起作用。在我插入到“www.javafx.com”sample.fxml的超链接之前,应用程序一直运行 配置了SDK,设置了VM选项,我从javafx导入了所有的包。我还设置了环境变量。所有设置似乎正常..错误是:

  • 问题: 运行JavaFX应用程序jar失败,尽管主类成功运行,显示一个GUI窗口。 IntelliJ创意社区版2021.2.1 Liberica Full JDK 11.0.12+7x86 64位for Windows(已包含JavaFX) Windows 10 Home 64bit 项目结构(IDE生成,未修改): 源代码: null null null null null 异常堆栈跟踪(运行应

  • 我已经使用向导生成了一个带有导航抽屉的应用程序。 当我选择“运行”“应用”或“调试”“应用”时,我会得到以下错误: 生成签名APK...选项工作,它创建的签名应用程序,可以安装在电话和工作。也可以使或重建项目在调试或发布中工作,没有错误。 null

  • 汇率设置即针对平台纳管的账单币种之间进行汇率转换,支持在不同时期设置不同的汇率。后续可在费用的其他页面将不同币种的账单以指定汇率转换成同一币种的费用进行展示。 当在全局配置中开启了“多币种账单合并展示开关”后,才会在费用-高级配置中出现汇率配置的菜单。 汇率设置即针对平台纳管的账单币种之间进行汇率转换,支持在不同时期设置不同的汇率。后续可在费用的其他页面将不同币种的账单以指定汇率转换成同一币种的费

  • 说明 微信支付-查询汇率SDK。 官方文档:https://pay.weixin.qq.com/wiki/doc/api/external/micropay.php?chapter=9_15&index=8 类 请求参数类 请求参数 类名:\Yurun\PaySDK\Weixin\ExchageRate\Request 属性 名称 类型 说明 $_apiMethod string 接口名称 $su

  • 说明 支付宝境外在线支付-汇率查询 官方文档:https://global.alipay.com/service/website_split_cn/19 类 请求参数类 请求参数 类名:\Yurun\PaySDK\AlipayCrossBorder\Online\ExchageRate\Request 属性 名称 类型 说明 $service string 接口名称 $_syncResponseN