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

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


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)

        ... 11 more

Logcat 导航到这行代码


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

我假设这是在接口中调用方法,我在 @Get 方法中错误地命名了 String 。


慕桂英3389331
浏览 95回答 1
1回答

宝慕林4294392

如网站上所述,您可能会收到这种类型的 JSON 响应{&nbsp; "base": "EUR",&nbsp; "date": "2018-04-08",&nbsp; "rates": {&nbsp; &nbsp; "CAD": 1.565,&nbsp; &nbsp; "CHF": 1.1798,&nbsp; &nbsp; "GBP": 0.87295,&nbsp; &nbsp; "SEK": 10.2983,&nbsp; &nbsp; "EUR": 1.092,&nbsp; &nbsp; "USD": 1.2234,&nbsp; &nbsp; ...&nbsp; }}所以要解析这种类型的 JSON,你的 POJO 类应该是这样的public class MyPojo{&nbsp; &nbsp; private String date;&nbsp; &nbsp; private Rates rates;&nbsp; &nbsp; private String base;&nbsp; &nbsp; public String getDate ()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return date;&nbsp; &nbsp; }&nbsp; &nbsp; public void setDate (String date)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.date = date;&nbsp; &nbsp; }&nbsp; &nbsp; public Rates getRates ()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return rates;&nbsp; &nbsp; }&nbsp; &nbsp; public void setRates (Rates rates)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.rates = rates;&nbsp; &nbsp; }&nbsp; &nbsp; public String getBase ()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return base;&nbsp; &nbsp; }&nbsp; &nbsp; public void setBase (String base)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.base = base;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return "ClassPojo [date = "+date+", rates = "+rates+", base = "+base+"]";&nbsp; &nbsp; }public class Rates{&nbsp; &nbsp; private String CHF;&nbsp; &nbsp; private String EUR;&nbsp; &nbsp; private String GBP;&nbsp; &nbsp; private String CAD;&nbsp; &nbsp; private String USD;&nbsp; &nbsp; private String SEK;&nbsp; &nbsp; public String getCHF ()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return CHF;&nbsp; &nbsp; }&nbsp; &nbsp; public void setCHF (String CHF)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.CHF = CHF;&nbsp; &nbsp; }&nbsp; &nbsp; public String getEUR ()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return EUR;&nbsp; &nbsp; }&nbsp; &nbsp; public void setEUR (String EUR)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.EUR = EUR;&nbsp; &nbsp; }&nbsp; &nbsp; public String getGBP ()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return GBP;&nbsp; &nbsp; }&nbsp; &nbsp; public void setGBP (String GBP)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.GBP = GBP;&nbsp; &nbsp; }&nbsp; &nbsp; public String getCAD ()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return CAD;&nbsp; &nbsp; }&nbsp; &nbsp; public void setCAD (String CAD)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.CAD = CAD;&nbsp; &nbsp; }&nbsp; &nbsp; public String getUSD ()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return USD;&nbsp; &nbsp; }&nbsp; &nbsp; public void setUSD (String USD)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.USD = USD;&nbsp; &nbsp; }&nbsp; &nbsp; public String getSEK ()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return SEK;&nbsp; &nbsp; }&nbsp; &nbsp; public void setSEK (String SEK)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.SEK = SEK;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return "ClassPojo [CHF = "+CHF+", EUR = "+EUR+", GBP = "+GBP+", CAD = "+CAD+", USD = "+USD+", SEK = "+SEK+"]";&nbsp; &nbsp; }}}让我知道它是否有效
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java