Advertisement
tdudzik

Retrofit

Sep 9th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.57 KB | None | 0 0
  1. import com.google.common.base.MoreObjects;
  2. import com.google.inject.AbstractModule;
  3. import com.google.inject.Guice;
  4. import com.google.inject.Injector;
  5. import com.google.inject.Provides;
  6. import okhttp3.ConnectionPool;
  7. import okhttp3.OkHttpClient;
  8. import retrofit2.*;
  9. import retrofit2.http.GET;
  10. import retrofit2.http.Headers;
  11. import retrofit2.http.Path;
  12.  
  13. import javax.inject.Inject;
  14. import java.io.IOException;
  15. import java.util.List;
  16. import java.util.concurrent.TimeUnit;
  17.  
  18. class Post {
  19.  
  20.     private final long userId;
  21.     private final long id;
  22.     private final String title;
  23.     private final String body;
  24.  
  25.     public Post(long userId, long id, String title, String body) {
  26.         this.userId = userId;
  27.         this.id = id;
  28.         this.title = title;
  29.         this.body = body;
  30.     }
  31.  
  32.     @Override
  33.     public String toString() {
  34.         return MoreObjects.toStringHelper(Post.class)
  35.                 .add("userId", userId)
  36.                 .add("id", id)
  37.                 .add("title", title)
  38.                 .add("body", body)
  39.                 .toString();
  40.     }
  41.  
  42. }
  43.  
  44. interface PostClient {
  45.  
  46.     @Headers({
  47.             "Accept: application/json"
  48.     })
  49.     @GET("posts/{id}")
  50.     Call<Post> findPostById(@Path("id") long id);
  51.  
  52.     @Headers({
  53.             "Accept: application/json"
  54.     })
  55.     @GET("posts")
  56.     Call<List<Post>> findAllPosts();
  57.  
  58. }
  59.  
  60. class App {
  61.  
  62.     private final Retrofit retrofit;
  63.  
  64.     @Inject
  65.     public App(Retrofit retrofit) {
  66.         this.retrofit = retrofit;
  67.     }
  68.  
  69.     public void test() {
  70.         PostClient postClient = retrofit.create(PostClient.class);
  71.  
  72.         // sync
  73.         try {
  74.             Response<Post> response = postClient.findPostById(12).execute();
  75.             System.out.println(response.body());
  76.             System.out.println("-------------------");
  77.         } catch (IOException e) {
  78.             System.out.println("Error. :(");
  79.             System.out.println("-------------------");
  80.         }
  81.  
  82.         // async
  83.         postClient.findAllPosts().enqueue(new Callback<List<Post>>() {
  84.             @Override
  85.             public void onResponse(Call<List<Post>> call, Response<List<Post>> response) {
  86.                 System.out.println(response.body().get(0));
  87.                 System.out.println("-------------------");
  88.             }
  89.  
  90.             @Override
  91.             public void onFailure(Call<List<Post>> call, Throwable throwable) {
  92.                 System.out.println("Error. :(");
  93.                 System.out.println("-------------------");
  94.             }
  95.         });
  96.     }
  97.  
  98. }
  99.  
  100. class AppModule extends AbstractModule {
  101.  
  102.     @Override
  103.     protected void configure() {
  104.     }
  105.  
  106.     @Provides
  107.     public Retrofit provideRetrofit() {
  108.         ConnectionPool connectionPool = new ConnectionPool(5, 60, TimeUnit.SECONDS);
  109.  
  110.         OkHttpClient httpClient = new OkHttpClient.Builder()
  111.                 .connectTimeout(100, TimeUnit.MILLISECONDS)
  112.                 .readTimeout(500, TimeUnit.MILLISECONDS)
  113.                 .connectionPool(connectionPool)
  114.                 .build();
  115.  
  116.         Retrofit retrofit = new Retrofit.Builder()
  117.                 .baseUrl("http://jsonplaceholder.typicode.com/")
  118.                 .client(httpClient)
  119.                 .addConverterFactory(GsonConverterFactory.create())
  120.                 .build();
  121.  
  122.         return retrofit;
  123.     }
  124.  
  125. }
  126.  
  127. public class Application {
  128.  
  129.     public static void main(String[] args) throws IOException {
  130.         Injector injector = Guice.createInjector(new AppModule());
  131.         App app = injector.getInstance(App.class);
  132.         app.test();
  133.     }
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement