Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.google.common.base.MoreObjects;
- import com.google.inject.AbstractModule;
- import com.google.inject.Guice;
- import com.google.inject.Injector;
- import com.google.inject.Provides;
- import okhttp3.ConnectionPool;
- import okhttp3.OkHttpClient;
- import retrofit2.*;
- import retrofit2.http.GET;
- import retrofit2.http.Headers;
- import retrofit2.http.Path;
- import javax.inject.Inject;
- import java.io.IOException;
- import java.util.List;
- import java.util.concurrent.TimeUnit;
- class Post {
- private final long userId;
- private final long id;
- private final String title;
- private final String body;
- public Post(long userId, long id, String title, String body) {
- this.userId = userId;
- this.id = id;
- this.title = title;
- this.body = body;
- }
- @Override
- public String toString() {
- return MoreObjects.toStringHelper(Post.class)
- .add("userId", userId)
- .add("id", id)
- .add("title", title)
- .add("body", body)
- .toString();
- }
- }
- interface PostClient {
- @Headers({
- "Accept: application/json"
- })
- @GET("posts/{id}")
- Call<Post> findPostById(@Path("id") long id);
- @Headers({
- "Accept: application/json"
- })
- @GET("posts")
- Call<List<Post>> findAllPosts();
- }
- class App {
- private final Retrofit retrofit;
- @Inject
- public App(Retrofit retrofit) {
- this.retrofit = retrofit;
- }
- public void test() {
- PostClient postClient = retrofit.create(PostClient.class);
- // sync
- try {
- Response<Post> response = postClient.findPostById(12).execute();
- System.out.println(response.body());
- System.out.println("-------------------");
- } catch (IOException e) {
- System.out.println("Error. :(");
- System.out.println("-------------------");
- }
- // async
- postClient.findAllPosts().enqueue(new Callback<List<Post>>() {
- @Override
- public void onResponse(Call<List<Post>> call, Response<List<Post>> response) {
- System.out.println(response.body().get(0));
- System.out.println("-------------------");
- }
- @Override
- public void onFailure(Call<List<Post>> call, Throwable throwable) {
- System.out.println("Error. :(");
- System.out.println("-------------------");
- }
- });
- }
- }
- class AppModule extends AbstractModule {
- @Override
- protected void configure() {
- }
- @Provides
- public Retrofit provideRetrofit() {
- ConnectionPool connectionPool = new ConnectionPool(5, 60, TimeUnit.SECONDS);
- OkHttpClient httpClient = new OkHttpClient.Builder()
- .connectTimeout(100, TimeUnit.MILLISECONDS)
- .readTimeout(500, TimeUnit.MILLISECONDS)
- .connectionPool(connectionPool)
- .build();
- Retrofit retrofit = new Retrofit.Builder()
- .baseUrl("http://jsonplaceholder.typicode.com/")
- .client(httpClient)
- .addConverterFactory(GsonConverterFactory.create())
- .build();
- return retrofit;
- }
- }
- public class Application {
- public static void main(String[] args) throws IOException {
- Injector injector = Guice.createInjector(new AppModule());
- App app = injector.getInstance(App.class);
- app.test();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement