Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.iadvize.geniusquest.activities;
- import android.app.Activity;
- import android.os.Bundle;
- import com.iadvize.geniusquest.R;
- import retrofit2.Retrofit;
- import retrofit2.http.POST;
- import rx.Observable;
- import rx.android.schedulers.AndroidSchedulers;
- import rx.functions.Action1;
- import rx.schedulers.Schedulers;
- public class MyActivity extends Activity {
- class PostResponse {
- // Here should be the java object description of the post response
- }
- // This is your API definition
- interface MyApi {
- @POST("/myurl")
- Observable<PostResponse> myPostRequest();
- }
- private final static String SERVER_ENDPOINT = "http://myserver.com";
- @Override
- protected void onCreate(final Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.root);
- final Retrofit adapter = new Retrofit.Builder()
- .baseUrl(SERVER_ENDPOINT)
- .build();
- final MyApi api = adapter.create(MyApi.class);
- api.myPostRequest()
- .subscribeOn(Schedulers.newThread())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(new Action1<PostResponse>() {
- @Override
- public void call(final PostResponse postResponse) {
- // On POST success this will be called (with the response data in parameter)
- }
- },
- new Action1<Throwable>() {
- @Override
- public void call(final Throwable throwable) {
- // On POST error, this will be called with the error in parameter
- }
- }
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement