Android开发求助:URL获取JSON数据无法在RecyclerView中显示
Hey Vinay, let's break down why your activity_swami is showing blank when fetching JSON from a URL—since static local data works, your RecyclerView setup is probably solid, so the issue is likely tied to network fetching, JSON parsing, or how you're updating the adapter after getting data. Let's go through the key checks and fixes step by step:
Android blocks network operations without explicit permissions, and restricts plain HTTP requests by default on newer versions:
- Add these permissions to your
AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
- If your JSON URL uses HTTP (not HTTPS), create a
res/xml/network_security_config.xmlfile to allow cleartext traffic:
<?xml version="1.0" encoding="utf-8"?> <network-security-config> <base-config cleartextTrafficPermitted="true" /> </network-security-config>
Then link it in your AndroidManifest.xml's application tag:
android:networkSecurityConfig="@xml/network_security_config"
Android throws a NetworkOnMainThreadException if you try to fetch data directly on the UI thread. Use a background thread + main thread update for UI changes. For beginners, coroutines are a simple, modern approach:
First add coroutine dependencies to your app/build.gradle:
dependencies { implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3" }
Then update your swami.java to handle the request safely:
import kotlinx.coroutines.CoroutineScope; import kotlinx.coroutines.Dispatchers; import kotlinx.coroutines.launch; import kotlinx.coroutines.withContext; import android.widget.Toast; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; // Inside your swami Activity private void fetchJsonFromUrl() { CoroutineScope(Dispatchers.IO).launch(() -> { try { // Replace with your actual JSON URL URL url = new URL("https://your-json-api-url.com/data"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuilder responseBuilder = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { responseBuilder.append(line); } reader.close(); conn.disconnect(); // Parse JSON and update UI on main thread String jsonResponse = responseBuilder.toString(); withContext(Dispatchers.Main, () -> { parseJsonAndUpdateRecyclerView(jsonResponse); }); } catch (Exception e) { e.printStackTrace(); // Show error toast on main thread withContext(Dispatchers.Main, () -> { Toast.makeText(swami.this, "Failed to load data", Toast.LENGTH_SHORT).show(); }); } }); }
Since local data works, your adapter/viewholder is fine—double-check that your parsing matches the JSON structure exactly (field names are case-sensitive!):
import org.json.JSONArray; import org.json.JSONObject; import java.util.ArrayList; import java.util.List; // Replace YourModelClass with your actual data model private void parseJsonAndUpdateRecyclerView(String json) { try { JSONArray jsonArray = new JSONArray(json); List<YourModelClass> dataList = new ArrayList<>(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject itemObj = jsonArray.getJSONObject(i); // Match keys to your JSON's actual field names String title = itemObj.getString("card_title"); String content = itemObj.getString("card_content"); dataList.add(new YourModelClass(title, content)); } // Update RecyclerView adapter YourRecyclerAdapter adapter = new YourRecyclerAdapter(dataList); RecyclerView recyclerView = findViewById(R.id.your_recycler_view_id); recyclerView.setAdapter(adapter); } catch (Exception e) { e.printStackTrace(); Toast.makeText(this, "Failed to parse data", Toast.LENGTH_SHORT).show(); } }
Make sure you've set up the LayoutManager in swami.java's onCreate method (local data works, but double-check anyway):
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_swami); RecyclerView recyclerView = findViewById(R.id.your_recycler_view_id); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setHasFixedSize(true); // Improves performance // Call your fetch method here fetchJsonFromUrl(); }
- Check Logcat for errors: Search for
NetworkOnMainThreadException,JSONException, or IO errors to pinpoint failures - Test your JSON URL in a browser/Postman first to confirm it returns valid JSON
- Replace your URL with a public test API (like
https://jsonplaceholder.typicode.com/users) to rule out issues with your endpoint
内容的提问来源于stack exchange,提问作者vinay

