[英]onResponse() not called in REST call for Android using retrofit2
所以,我正在嘗試連接到 Scryfall 的 API 並進行自動完成調用。 我已經能夠正確使用他們的另一個電話,但我認為這是我遇到問題的地方。
這是調用: https://api.scryfall.com/cards/autocomplete?q=fire q 是查詢,它將返回最多 20 個可以用單詞“fire”自動完成的項目的列表。
{
"object":"catalog",
"total_values":20,
"data": [
"Fire // Ice","Fire Imp","Firefly","Fire Whip","Fire Ants","Firebolt","Fireball","Fire Drake","Fire Snake","Firespout","Firestorm","Fireblast","Fire-Field Ogre","Fire Urchin","Fire Bowman","Fire Dragon","Fire at Will","Fire Ambush","Firemaw Kavu","Fire Juggler"
]
}
我正在為 android 使用改造 2。
這是我的一些代碼。
這是我的端點接口
public interface ScryfallEndPoints {
//https://api.scryfall.com/cards/named?fuzzy=
@GET("cards/named")
Call<Card> getCard(
@Query(value=("fuzzy")) String name);
//https://api.scryfall.com/cards/autocomplete?q=
@GET("cards/autocomplete")
Call<Card> getCardAutoComplete(
@Query(value=("q")) String name);
}
這是我在活動中用來執行調用的方法。
private void loadCardList()
{
final ScryfallEndPoints apiService =
APIClient.getClient().create(ScryfallEndPoints.class);
Call<Map<String, String>> call = apiService.getCardAutoComplete(str);
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
call.enqueue(new Callback<Map<String, String>>()
{
@Override
public void onResponse(Call<Map<String, String>> call, Response<Map<String, String>> response)
{
Toast.makeText(SuggestionResults.this, "onResponse()", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<Map<String, String>> call, Throwable t)
{
Toast.makeText(SuggestionResults.this, "onFailure()", Toast.LENGTH_SHORT).show();
}
});
//tv.setText(str);
}
這是我的 model class 的一部分的方法。
@SerializedName("data")
private Map<String, String> cardList;
public Map<String, String> getCardList() {return cardList;}
所以,我覺得我嘗試訪問 model class 中的數據的方式肯定有問題,也許是我在界面中設置它的方式。 當我撥打電話時,它不會失敗,所以我沒有要顯示的錯誤日志,我只知道它會調用 onFailure() 方法,我不知道為什么。 我主要需要弄清楚這一點,然后我才能着手填充列表。 此外,如果有一種方法可以讓我看到更多關於我正在撥打的電話的情況,那也很棒。 謝謝!
@GET("cards/autocomplete")
Call<Card> getCardAutoComplete(
@Query(value=("q")) String name);
}
然后調用部分
//Call<Map<String, String>> call = apiService.getCardAutoComplete(str);
//It returns Call of Card type, hence it will be as follows
Call<Card> call = apiService.getCardAutoComplete(str);
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
call.enqueue(new Callback<Card>()
{
@Override
public void onResponse(Call<Card> call, Response<Card> response)
{
Toast.makeText(SuggestionResults.this, "onResponse()", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<Card> call, Throwable t)
{
Toast.makeText(SuggestionResults.this, "onFailure()", Toast.LENGTH_SHORT).show();
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.