android - Retrofit2 的问题 - Android
问题描述
当 json 数据如下所示时,我无法检索数据:
{
"": "",
"": ""
}
但一切都是这样工作的:
[
{
"": "",
"": ""
}
]
我的问题是我真的需要像第一个选项一样检索我的数据。
public interface API {
String BASE_URL = "https://cloud-functions-d378b.firebaseapp.com/";
@GET("PLAYER_API.json")
Call<List<Players>> getPlayer();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player_info);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API.BASE_URL)
.addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
.build();
API api = retrofit.create(API.class);
Call<List<Players>> call = api.getPlayer();
onResponse will be executed
call.enqueue(new Callback<List<Players>>() {
@Override
public void onResponse(Call<List<Players>> call, Response<List<Players>> response) {
List<Players> playersList = response.body();
String[] players = new String[playersList.size()];
for (int i = 0; i < playersList.size(); i++) {
}
我希望能够以这种格式检索我的数据:
{
"": "",
"": ""
}
解决方案
只需更改 api 调用的声明:
Call<Players> getPlayer();
推荐阅读
- machine-learning - 如何使用歌词生成 Word Embeddings/Word2Vec 的拼写变化(尤其是俚语)?
- php - 来自特定国家/地区的 Chrome 中的 wordpress err_connection_reset
- scala - 最后一列名称包含空格时,Spark 无法读取 CSV
- javascript - 为什么我的 Firebase 应用的表格中出现“未定义”?
- javascript - 在获取数据时将 null 或 undefined 传递给组件是否更好?
- sql-server - 将文件移动到子文件夹 ssis 包
- android - 如何使用 Local Notifications / Notification.Builder 在 Android 上的 Xamarin.Forms 中播放自定义声音
- javascript - 如何执行位于符号链接目录内的脚本?
- java - java.io.FileNotFoundException(试图反序列化)
- android - 如何使用 ViewPager 在特定页面上实现水平和垂直过渡?