2016-11-03 77 views
-1

改裝多個請求我創建應用程序調用API的招式與改造圖書館和recyclerViewe DB時的用戶選擇查看詳細的電影 現在我ritrofit實施recyclerView電影展活動,以創建獲取熱門電影數據, 這MoviesApiService類:在安卓

public interface MoviesApiService { 
    @GET("/movie/popular") 
    void getPopularMovies(Callback<Movie.MovieResult> cb); } 

這個模型類:

package com.walkatheri.popularmovies; 

import android.os.Parcel; 
import android.os.Parcelable; 

import com.google.gson.annotations.SerializedName; 

import java.util.List; 

/** 

*/ 
public class Movie implements Parcelable{ 
    private String title; 
    @SerializedName("poster_path") 
    private String poster; 
    @SerializedName("overview") 
    private String description; 
    @SerializedName("backdrop_path") 
    private String backdrop; 

    public Movie() {} 

    protected Movie(Parcel in) { 
     title = in.readString(); 
     poster = in.readString(); 
     description = in.readString(); 
     backdrop = in.readString(); 
    } 

    public static final Creator<Movie> CREATOR = new Creator<Movie>() { 
     @Override 
     public Movie createFromParcel(Parcel in) { 
      return new Movie(in); 
     } 

     @Override 
     public Movie[] newArray(int size) { 
      return new Movie[size]; 
     } 
    }; 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public String getPoster() { 
     return "http://image.tmdb.org/t/p/w500" + poster; 
    } 

    public void setPoster(String poster) { 
     this.poster = poster; 
    } 

    public String getDescription() { 
     return description; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 

    public String getBackdrop() { 
     return "http://image.tmdb.org/t/p/w500" + backdrop; 
    } 

    public void setBackdrop(String backdrop) { 
     this.backdrop = backdrop; 
    } 

    @Override 
    public int describeContents() { 
     return 0; 
    } 

    @Override 
    public void writeToParcel(Parcel parcel, int i) { 
     parcel.writeString(title); 
     parcel.writeString(poster); 
     parcel.writeString(description); 
     parcel.writeString(backdrop); 
    } 

    public static class MovieResult { 
     private List<Movie> results; 

     public List<Movie> getResults() { 
      return results; 
     } 
    } 
} 

類細節的電影: 公共類MovieDetai lActivity擴展了AppCompatActivity {public static final String EXTRA =「movie」;

private Movie mMovie; 
    ImageView backdrop; 
    ImageView poster; 
    TextView title; 
    TextView description; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_movie_detail); 
     if (getIntent().hasExtra(EXTRA)) { 
      mMovie = getIntent().getParcelableExtra(EXTRA); 
     } else { 
      throw new IllegalArgumentException("Detail activity must receive a movie parcelable"); 
     } 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     CollapsingToolbarLayout toolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout); 
     toolbarLayout.setTitle(mMovie.getTitle()); 

     backdrop = (ImageView) findViewById(R.id.backdrop); 
     title = (TextView) findViewById(R.id.movie_title); 
     description = (TextView) findViewById(R.id.movie_description); 
     poster = (ImageView) findViewById(R.id.movie_poster); 

     title.setText(mMovie.getTitle()); 
     description.setText(mMovie.getDescription()); 
     Picasso.with(this) 
       .load(mMovie.getPoster()) 
       .into(poster); 
     Picasso.with(this) 
       .load(mMovie.getBackdrop()) 
       .into(backdrop); 
    } 
} 

這MainActivity類別:

public class MainActivity extends AppCompatActivity { 
    private RecyclerView mRecyclerView; 
    private MoviesAdapter mAdapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView); 
     mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2)); 
     mAdapter = new MoviesAdapter(this); 
     mRecyclerView.setAdapter(mAdapter); 
     getPopularMovies(); 
    } 

    private void getPopularMovies() { 
     RestAdapter restAdapter = new RestAdapter.Builder() 
       .setEndpoint("http://api.themoviedb.org/3") 
       .setRequestInterceptor(new RequestInterceptor() { 
        @Override 
        public void intercept(RequestFacade request) { 
         request.addEncodedQueryParam("api_key", "Key_API"); 
        } 
       }) 
       .setLogLevel(RestAdapter.LogLevel.FULL) 
       .build(); 
     MoviesApiService service = restAdapter.create(MoviesApiService.class); 
     service.getPopularMovies(new Callback<Movie.MovieResult>() { 
      @Override 
      public void success(Movie.MovieResult movieResult, Response response) { 
       mAdapter.setMovieList(movieResult.getResults()); 
      } 

      @Override 
      public void failure(RetrofitError error) { 
       error.printStackTrace(); 
      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     int id = item.getItemId(); 

       if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
    public static class MovieViewHolder extends RecyclerView.ViewHolder { 
     public ImageView imageView; 
     public MovieViewHolder(View itemView) { 
      super(itemView); 
      imageView = (ImageView) itemView.findViewById(R.id.imageView); 
     } 
    } 
    public static class MoviesAdapter extends RecyclerView.Adapter<MovieViewHolder> { 
     private List<Movie> mMovieList; 
     private LayoutInflater mInflater; 
     private Context mContext; 

     public MoviesAdapter(Context context) { 
      this.mContext = context; 
      this.mInflater = LayoutInflater.from(context); 
     } 

     @Override 
     public MovieViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) { 
      View view = mInflater.inflate(R.layout.row_movie, parent, false); 
      final MovieViewHolder viewHolder = new MovieViewHolder(view); 
      view.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        int position = viewHolder.getAdapterPosition(); 
        Intent intent = new Intent(mContext, MovieDetailActivity.class); 
        intent.putExtra(MovieDetailActivity.EXTRA, mMovieList.get(position)); 
        mContext.startActivity(intent); 
       } 
      }); 
      return viewHolder; 
     } 

     @Override 
     public void onBindViewHolder(MovieViewHolder holder, int position) { 
      Movie movie = mMovieList.get(position); 
      Picasso.with(mContext) 
        .load(movie.getPoster()) 
        .placeholder(R.color.colorAccent) 
        .into(holder.imageView); 
     } 

     @Override 
     public int getItemCount() { 
      return (mMovieList == null) ? 0 : mMovieList.size(); 
     } 

     public void setMovieList(List<Movie> movieList) { 
      this.mMovieList = new ArrayList<>(); 
      this.mMovieList.addAll(movieList); 
      notifyDataSetChanged(); 
     } 
    } 
} 

現在如何能夠從https://developers.themoviedb.org獲取兩個熱門電影數據和高評級的電影數據?你可以幫我嗎 ?

回答

0

您的MainActivity的方法叫getPopularMovies(),你叫service.getPopularMovies(),它使用MoviesApiService@GET("/movie/popular")。你可以創建一個高級電影相似的API,是這樣的:

@GET("/movie/high_rated") void getHighRatedMovies(Callback<Movie.MovieResult> cb);

然後創建一個類似getHighRatedMovies()方法和根據什麼用戶選擇,使該方法的調用。唯一的區別是你會打電話service.getHighRatedMovies而不是service.getPopularMovies()

要簡化代碼到一種方法,您可以製作一個名爲getMovies()的方法,然後在初始化service.(something)時設置正確的方法調用。