網絡請求返回 JSON,Logcat 打印顯示解析正常,但 ListView 仍然空白無數據。
adapter.notifyDataSetChanged()。原代碼中該行被註釋。// 在 handleMessage 最後添加
runOnUiThread(() -> adapter.notifyDataSetChanged());
NewsAdapter.getView() 中呼叫 HttpUtils.setPicBitmap(thumb_value, null),第二參數傳 null 導致圖片下載地址為空。應傳入 news.getCustom_fields() 或具體圖片 URL 字段。String imgUrl = news.getCustom_fields(); // 或解析出的 thumb_value
HttpUtils.setPicBitmap(thumb_value, imgUrl);
HttpUtils.setPicBitmap 內部開啟新線程下載圖片,但直接在子線程更新 UI (thumb_value.setImageBitmap) 會拋出異常且無顯示。需使用 runOnUiThread 或 Handler 回主線程設置圖片。// 修正 setPicBitmap
new Handler(Looper.getMainLooper()).post(() -> thumb_value.setImageBitmap(bitmap));
posts 數組中,且圖片地址在 custom_fields.thumb_value[0],原代碼直接讀 custom_fields 字符串,導致圖片 URL 解析錯誤。建議使用 Gson 或 Moshi 自動映射實體類。// 使用 Gson 範例
Gson gson = new Gson();
NewsResponse response = gson.fromJson(jsonData, NewsResponse.class);
newsList.addAll(response.posts);
adapter.notifyDataSetChanged();
HttpURLConnection,簡化網絡層。