我从api获得了卡片:https://deckofcardsapi.com/
首先我下载一个套牌,接下来从这个套牌我得到卡片。这是一个问题。当我调试应用程序时,我有几个想法像java.lang.NumberFormatException:无效的双重:“ACE”,“QUEEN”,“JACK”,“KING”
但是当我得到这个时,这个工作:
这是我的方法卡片当然我不会让getter和setter更好地显示
public class Cards {
private boolean succes;
private List<Card> cards;
private String deck_id;
private int remaining;
}
public class Card {
private String image;
private int value;
private String suit;
private String code;
}
以下是从服务器获取Api的方法:
private void getCards(final String deck_id, final int count) {
retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
cardApi = retrofit.create(CardService.CardAPI.class);
cardApi.getCards(deck_id, count)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Cards>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
}
@Override
public void onNext(@NonNull Cards cards) {
Log.d("ARRRRRRRRRRRRAYYYYYYY", String.valueOf(cards));
cardsArray = cards.getArrayCards();
}
@Override
public void onError(@NonNull Throwable e) {
Log.d("FADSFAFASDFAFS", String.valueOf(e));
}
@Override
public void onComplete() {
Toast.makeText(getApplicationContext(), String.valueOf(cardsArray), Toast.LENGTH_SHORT).show();
}
});
}
和@Get
@GET("{deck_id}/draw")
Observable<Cards> getCards(@Path("deck_id") String deck_id, @Query("count") int count);
这是JSON
答案 0 :(得分:1)
Card的值并不总是int。 4张牌将具有值&#34; Ace&#34; ,&#34; King&#34; &#34;大号&#34;和杰克&#34;。这些是字符串。这就是为什么你得到数字格式异常
public class Card {
private String image;
private int value;//this will not always be int.
private String suit;
private String code;
}
要正确处理,Card类应该看起来像
public class Card{
private String image;
private String value;
private String suit;
private String code;
}