Retrofit 2.3请求JSON解析后List为空问题求助
Hey there, I’ve been stuck in this exact situation before—wasting hours staring at empty Retrofit lists even when the API says "Successful." Let’s walk through the most likely fixes step by step:
1. Verify JSON Structure ↔ Model Class Match (The #1 Culprit)
9 times out of 10, this is where the problem lives. If your PHP API returns a wrapped JSON object like this:
{"message":"Successful","data":[{"id":1,"name":"Alice"},{"id":2,"name":"Bob"}]}
You can’t directly parse this into a List<User>—you need a top-level response model to wrap the list. For example:
public class UserListResponse { @SerializedName("message") private String message; @SerializedName("data") // Match the exact key from your JSON private List<User> userList; // Add getters/setters here }
Double-check:
- Field names match exactly (case-sensitive unless you use
@SerializedName) - Nested arrays are mapped correctly to
Listtypes - No missing fields or extra fields that Gson might choke on (use
@Exposeif you need to ignore certain fields)
2. Fix Your Retrofit Interface Definition
If you’re trying to return Call<List<User>> directly but the JSON is wrapped in an object, Retrofit will silently fail and return an empty list. Update your interface to use the wrapper model:
// Wrong (for wrapped JSON) @GET("your-endpoint") Call<List<User>> getUsers(); // Correct @GET("your-endpoint") Call<UserListResponse> getUsers();
3. Enable Retrofit Logging to See the Real Response
You might think the JSON is correct, but hidden whitespace, incorrect escaping, or unexpected fields could be breaking parsing. Add OkHttp’s logging interceptor to see the full raw response:
HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); logging.setLevel(HttpLoggingInterceptor.Level.BODY); // Logs full request/response OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(logging) .build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .client(client) .addConverterFactory(GsonConverterFactory.create()) .build();
Check Logcat for the exact JSON returned—this will reveal if the structure matches what you’re expecting.
4. Test Manual Parsing with Gson
Copy the raw JSON from the logs and test parsing it manually to isolate the issue:
String rawJson = "{\"message\":\"Successful\",\"data\":[{...}]}"; // Paste your log JSON here UserListResponse testResponse = new Gson().fromJson(rawJson, UserListResponse.class); if (testResponse.getUserList() != null) { Log.d("DEBUG", "Manual parse worked! List size: " + testResponse.getUserList().size()); } else { Log.d("DEBUG", "Manual parse failed—model mismatch!"); }
If this returns an empty list, your model class is definitely misaligned with the JSON. If it works, the issue is in your Retrofit configuration.
5. Check PHP’s Content-Type Header
Make sure your PHP endpoint is setting the correct Content-Type header. If it returns text/html instead of application/json, Retrofit’s Gson converter might ignore the response:
header('Content-Type: application/json'); // Add this before echoing JSON echo json_encode($responseData);
Content的提问来源于stack exchange,提问作者Joshua Weeks

