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

使用Retrofit从MySQL获取电影数据到RecyclerView无显示问题求助

Troubleshooting RecyclerView Not Showing Data with Retrofit & MySQL

Hey there! Let's figure out why your RecyclerView isn't displaying any movie data from your MySQL database using Retrofit. I'll walk through common issues and fixes based on what you've shared so far.

1. First: Validate Your Retrofit Setup & API Response

  • Make sure Retrofit is configured correctly: Double-check that you added the Gson converter factory when building your Retrofit instance—this is critical for parsing JSON into your MovieListsModels objects:
    Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(url)
        .addConverterFactory(GsonConverterFactory.create())
        .build();
    
  • Test your API directly: Use tools like Postman or logcat to confirm your endpoint http://192.168.1.109/stu/ returns valid JSON that matches your MovieListsModels structure. For example, if your API returns a field named movie_name but your model uses movieName, you'll need to add the @SerializedName annotation to fix the mismatch:
    public class MovieListsModels {
        @SerializedName("movie_name")
        private String movieName;
        // rest of your fields
    }
    
  • Add logging to your Retrofit callback: This will tell you if data is actually being fetched successfully:
    YourApiInterface apiService = retrofit.create(YourApiInterface.class);
    apiService.getMovies().enqueue(new Callback<List<MovieListsModels>>() {
        @Override
        public void onResponse(Call<List<MovieListsModels>> call, Response<List<MovieListsModels>> response) {
            if (response.isSuccessful() && response.body() != null) {
                Log.d("RetrofitDebug", "Fetched " + response.body().size() + " movies");
                movies = response.body();
                adapter.notifyDataSetChanged(); // Update the adapter with new data
            } else {
                Log.e("RetrofitError", "Response failed: " + response.message());
                // Update your incomplete "Er" TextView here to show error messages
            }
        }
    
        @Override
        public void onFailure(Call<List<MovieListsModels>> call, Throwable t) {
            Log.e("RetrofitError", "Request failed: " + t.getMessage());
        }
    });
    

2. Check RecyclerView Initialization

It's easy to miss key setup steps for RecyclerView—confirm you did these in onCreate:

recyclerViewMovies = findViewById(R.id.your_recycler_view_id);
// You MUST set a LayoutManager (LinearLayoutManager, GridLayoutManager, etc.)
recyclerViewMovies.setLayoutManager(new LinearLayoutManager(this));
// Initialize your list and adapter BEFORE making the API call
movies = new ArrayList<>();
adapter = new MoviesAdapter(movies, this);
recyclerViewMovies.setAdapter(adapter);

3. Verify Your MoviesAdapter Code

  • Ensure getItemCount() returns the actual size of your movie list:
    @Override
    public int getItemCount() {
        return movies != null ? movies.size() : 0;
    }
    
  • Double-check that onBindViewHolder correctly binds data to your item views. For example:
    @Override
    public void onBindViewHolder(MovieViewHolder holder, int position) {
        MovieListsModels currentMovie = movies.get(position);
        holder.movieTitleTextView.setText(currentMovie.getTitle());
        holder.movieYearTextView.setText(currentMovie.getReleaseYear());
        // Bind all other relevant fields
    }
    

4. Don't Forget Network Permissions

  • For Android 9 (API 28) and above, add this to your AndroidManifest.xml to allow HTTP traffic (since your endpoint uses http://):
    <application
        ...
        android:usesCleartextTraffic="true">
        ...
    </application>
    
  • Make sure you have the internet permission declared:
    <uses-permission android:name="android.permission.INTERNET" />
    

5. Rule Out Empty Data

Even if everything else is correct, your API might be returning an empty list. Use the log we added earlier to check response.body().size()—if it's 0, you'll need to debug your backend to ensure it's returning movie data properly.

Start with checking the Retrofit callback logs first—they'll give you the clearest clue about whether data is being fetched, parsed, or if something's failing along the way.

内容的提问来源于stack exchange,提问作者Omar H. Soliman

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 10:18:46