Retrofit

    

Lib:

implementation 'com.squareup.retrofit2:retrofit:2.7.0'implementation 'com.squareup.retrofit2:converter-gson:2.2.0'
 
classpath 'com.android.tools.build:gradle:3.5.1'
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
 
 
 

ApiClient.java

 
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ApiClient {
    public static final String BASE_URL = "http://api.themoviedb.org/3/";
    private static Retrofit retrofit = null;


    public static Retrofit getClient() {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}  
 
 

MainActivity.java

 
public class MainActivity extends AppCompatActivity {
    private final static String API_KEY = "8a9f5106a299b1e1e3c7c6265617bdc0";
    private static final String TAG = MainActivity.class.getSimpleName();
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (API_KEY.isEmpty()) {
            Toast.makeText(getApplicationContext(), "Please obtain your API KEY from themoviedb.org first!", Toast.LENGTH_LONG).show();
            return;
        }



        ApiInterface apiService =
                ApiClient.getClient().create(ApiInterface.class);

        Call<MoviesResponse> call = apiService.getTopRatedMovies(API_KEY);
        call.enqueue(new Callback<MoviesResponse>() {
            @Override            public void onResponse(Call<MoviesResponse> call, Response<MoviesResponse> response) {
                int statusCode = response.code();
                List<Movie> movies = response.body().getResults();
            }

            @Override            public void onFailure(Call<MoviesResponse> call, Throwable t) {
                // Log error here since request failed                Log.e(TAG, t.toString());
            }
        });
    }
}  
 
 

Movie.java

 

public class Movie {
    @SerializedName("poster_path")
    private String posterPath;
    @SerializedName("adult")
    private boolean adult;
   

    public Movie(String posterPath, boolean adult) {
        this.posterPath = posterPath;
        this.adult = adult;
        
    }

    public String getPosterPath() {
        return posterPath;
    }

    public void setPosterPath(String posterPath) {
        this.posterPath = posterPath;
    }

    public boolean isAdult() {
        return adult;
    }

    public void setAdult(boolean adult) {
        this.adult = adult;
    }

}
 
 

ApiInterface.java

 

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Headers;
import retrofit2.http.Path;
import retrofit2.http.Query;

public interface ApiInterface {
   /* @Headers({            "Accept: application/vnd.yourapi.v1.full+json",            "User-Agent: Your-App-Name"    })*/    @GET("movie/top_rated")
    Call<MoviesResponse> getTopRatedMovies(@Query("api_key") String apiKey);

    @GET("movie/{id}")
    Call<MoviesResponse> getMovieDetails(@Path("id") int id, @Query("api_key") String apiKey);
}

 

 


 
 
 
  
 
 

 

No comments:

Post a Comment