我使用Retrofit库从Database获取数据到recyclerview

时间:2018-03-17 00:47:30

标签: android http android-studio retrofit2 okhttp

我正在尝试从Mysql DB获取电影数据并将其显示到Recycler视图

但是当我运行应用程序时没有任何显示

这是我正在使用Retrofite Library的代码

但我无法将数据解析为Recycler视图

我使适配器和模型类通常像Json

  

MainActivity.class

public class MainActivity extends AppCompatActivity {

    private static final String url="http://192.168.1.109/stu/";
    RecyclerView recyclerViewMovies;
    List<MovieListsModels> movies;
    MoviesAdapter adapter;
    TextView Errortxt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Errortxt = (TextView)findViewById(R.id.txterror);
        recyclerViewMovies = (RecyclerView)findViewById(R.id.recyclerview);
        recyclerViewMovies.setHasFixedSize(true);
        recyclerViewMovies.setLayoutManager(new LinearLayoutManager(this));
        movies = new ArrayList<>();
        loadDatafromServer();
    }

    private void loadDatafromServer() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Api api = retrofit.create(Api.class);
        Call<MovieListsModels> call = api.ShowMoviesData();
        call.enqueue(new Callback<MovieListsModels>() {
            @Override
            public void onResponse(Call<MovieListsModels> call, Response<MovieListsModels> response) {
                try {
                    MovieListsModels movie = response.body();
                    adapter = new MoviesAdapter(MainActivity.this, (List<MovieListsModels>) movie);
                    recyclerViewMovies.setAdapter(adapter);
                } 
                catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<MovieListsModels> call, Throwable t) {
                Errortxt.setText(t.getMessage().toString());
            }
        });
    }

这是方法的接口

  

Api.class接口

public interface Api {
    @GET("config.php")
    Call<MovieListsModels> ShowMoviesData();
}
  

MovieLists.class

public class MovieListsModels {

    public MovieListsModels() {
    }

    int id;
    String movie_name;
    String movie_image;
    String movie_genre;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getMovie_name() {
        return movie_name;
    }

    public void setMovie_name(String movie_name) {
        this.movie_name = movie_name;
    }

    public String getMovie_image() {
        return movie_image;
    }

    public void setMovie_image(String movie_image) {
        this.movie_image = movie_image;
    }

    public String getMovie_genre() {
        return movie_genre;
    }

    public void setMovie_genre(String movie_genre) {
        this.movie_genre = movie_genre;
    }

    public MovieListsModels(int id, String movie_name, String movie_image, String movie_genre) {
        this.id = id;
        this.movie_name = movie_name;
        this.movie_image = movie_image;
        this.movie_genre = movie_genre;
    }

}
  

MovieAdapter.class

public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MovieHolderView>  {

    private Context mContext;
    private List<MovieListsModels> MovieList = new ArrayList<>();
    public MoviesAdapter(Context mContext, List<MovieListsModels> movieList) {
        this.mContext = mContext;
        MovieList = movieList;
    }

    @NonNull
    @Override
    public MovieHolderView onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
       View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item,parent,false);
       MovieHolderView holder = new MovieHolderView(view);
       return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MovieHolderView holder, int position) {
        MovieListsModels list = MovieList.get(position);
        holder.txtName.setText(list.getMovie_name());
        holder.txtGenre.setText(list.getMovie_genre());
        Picasso.get()
                .load(list.getMovie_image())
                .into(holder.imgMovie);
    }

    @Override
    public int getItemCount() {
        return MovieList.size();
    }

    public class MovieHolderView extends RecyclerView.ViewHolder {
        TextView txtName,txtGenre;
        ImageView imgMovie;
        public MovieHolderView(View itemView) {
             super(itemView);
             txtName =(TextView)itemView.findViewById(R.id.movieName);
             txtGenre =(TextView)itemView.findViewById(R.id.movieGenre);
             imgMovie =(ImageView)itemView.findViewById(R.id.movieImg);
        }
    }
}

3 个答案:

答案 0 :(得分:0)

如果您收到电影列表更好,因为您需要一个列表,我想

public void onResponse(Call<MovieListsModels> call, Response<MovieListsModels> response) {
            try {
                List<MovieListsModels> movie = response.body();
                adapter = new MoviesAdapter(MainActivity.this, movies);

我相信不执行notifyDataSetChanged,你可以这样添加:

    private Context mContext;
    private List<MovieListsModels> MovieList = new ArrayList<>();
    public MoviesAdapter(Context mContext, List<MovieListsModels> movieList) {
        this.mContext = mContext;
        MovieList = movieList;
        notifiyDataSetChanged();

答案 1 :(得分:0)

如果您拥有json response形式的{..},则表示您正在进行对象响应,并且您应该期待一个对象,即Call<YourObject>

如果您使用json response格式的[..],则表示您正在进行数组响应,您应该期待一个数组,即Call<List<YourObject>>

在你的情况下,我希望它是一个数组(第二种情况),所以根据@Guillodacosta的上述答案进行更改

答案 2 :(得分:0)

首先不要忘记在清单文件中添加互联网权限

<uses-permission android:name="android.permission.INTERNET" />

第二次尝试这个

Picasso.with(mContext).load(list.getMovie_image()).into(holder.imgMovie);