为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕桂英3389331 2022-11-02 15:07:10
所以我试图在应用程序上显示一些货币兑换,但我什至无法运行它,我遇到了这个错误:2019-05-01 14:34:57.260 6798-6798/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.example.myapplication, PID: 6798java.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 moreLogcat 导航到这行代码        Call<List<Currency>> call = currencyInterface.getCurrency();我假设这是在接口中调用方法,我在 @Get 方法中错误地命名了 String 。
查看完整描述

1 回答

?
宝慕林4294392

TA贡献2021条经验 获得超8个赞

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

    }

}

}

让我知道它是否有效


查看完整回答
反对 回复 2022-11-02
  • 1 回答
  • 0 关注
  • 94 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信