You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Retrofit请求JSON数组返回空值问题求助:数据类型误用排查

Fixing Retrofit Empty Body (200 OK) Issue & Clarifying Array vs ArrayList

Hey there! Let's break down why your Retrofit call is returning a 200 status but an empty response.body(), and clear up the confusion between using Array and ArrayList for your data.

The Core Problem: Incorrect Response Type & Missing Entity Mapping

First, let's look at your setup:

  • Your API returns a JSON array of coin objects
  • You're using Call<JSONArray> in your interface, but you have a Coinstats entity class that should map to each item in the array
  • Without proper mapping to your entity class, Retrofit can't parse the JSON correctly, leading to an empty body even with a 200 OK.

Step 1: Fix the Interface Definition

Instead of using JSONArray, you should use a List<Coinstats> as your response type. Retrofit (with Gson converter) will automatically parse the JSON array into a list of your entity objects. Here's the corrected interface:

@GET("api/v1/top-coins/{top}")
Call<List<Coinstats>> getStats(@Path("top") String top);

Step 2: Ensure Your Change Entity is Properly Serialized

Your Coinstats class has a Change field, but you didn't include its code. For parsing to work, this class also needs @SerializedName annotations for its fields. For example:

public class Change {
    @SerializedName("hour") @Expose private String hour;
    @SerializedName("day") @Expose private String day;
    @SerializedName("week") @Expose private String week;
    // Add getters and setters here
}

If this class is missing or incorrectly annotated, Gson can't parse the nested object, which will cause the entire response body to be null.

Step 3: Update Your Request Code

Now adjust your enqueue callback to use Callback<List<Coinstats>>:

call2.enqueue(new Callback<List<Coinstats>>() {
    @Override
    public void onResponse(Call<List<Coinstats>> call, Response<List<Coinstats>> response) {
        if (response.isSuccessful()) {
            List<Coinstats> coinStatsList = response.body();
            if (coinStatsList != null) {
                // Do something with your list of coins
                Log.w("Stats", "Fetched " + coinStatsList.size() + " coins");
            }
        } else {
            Log.w("Stats", "Request not successful: " + response.code());
        }
    }

    @Override
    public void onFailure(Call<List<Coinstats>> call, Throwable t) {
        Log.w("no Stats", t.toString());
    }
});

Array vs ArrayList: What Should You Use?

In Retrofit, when you declare List<Coinstats> as your response type, the underlying implementation will automatically use an ArrayList (since it's the most common List implementation). You don't need to specify ArrayList explicitly—using the List interface is better practice because it keeps your code flexible (you could swap implementations later if needed).

Extra Check: Make Sure You Have the Gson Converter

Don't forget to include the Gson converter dependency in your build.gradle (Module level) to let Retrofit handle JSON parsing:

implementation 'com.squareup.retrofit2:converter-gson:2.9.0' // Use the latest version

Without this converter, Retrofit can't map JSON to your entity classes, which will result in an empty body even with a 200 status.


内容的提问来源于stack exchange,提问作者user8875509

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:38:10