Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.88 KB | None | 0 0
  1. package com.iadvize.geniusquest.activities;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5.  
  6. import com.iadvize.geniusquest.R;
  7.  
  8. import retrofit2.Retrofit;
  9. import retrofit2.http.POST;
  10. import rx.Observable;
  11. import rx.android.schedulers.AndroidSchedulers;
  12. import rx.functions.Action1;
  13. import rx.schedulers.Schedulers;
  14.  
  15. public class MyActivity extends Activity {
  16.     class PostResponse {
  17.         // Here should be the java object description of the post response
  18.     }
  19.  
  20.     // This is your API definition
  21.     interface MyApi {
  22.         @POST("/myurl")
  23.         Observable<PostResponse> myPostRequest();
  24.     }
  25.  
  26.     private final static String SERVER_ENDPOINT = "http://myserver.com";
  27.  
  28.     @Override
  29.     protected void onCreate(final Bundle savedInstanceState) {
  30.         super.onCreate(savedInstanceState);
  31.         setContentView(R.layout.root);
  32.  
  33.         final Retrofit adapter = new Retrofit.Builder()
  34.                 .baseUrl(SERVER_ENDPOINT)
  35.                 .build();
  36.  
  37.         final MyApi api = adapter.create(MyApi.class);
  38.  
  39.         api.myPostRequest()
  40.                 .subscribeOn(Schedulers.newThread())
  41.                 .observeOn(AndroidSchedulers.mainThread())
  42.                 .subscribe(new Action1<PostResponse>() {
  43.                                @Override
  44.                                public void call(final PostResponse postResponse) {
  45.                                    // On POST success this will be called (with the response data in parameter)
  46.                                }
  47.                            },
  48.                         new Action1<Throwable>() {
  49.                             @Override
  50.                             public void call(final Throwable throwable) {
  51.                                 // On POST error, this will be called with the error in parameter
  52.                             }
  53.                         }
  54.                 );
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement