Last active
August 15, 2018 15:13
-
-
Save mayuroks/5c9ddbfebbef609c6a171657327039bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
private ApiService apiService = HttpClient.getApiService(); | |
private TextView tvStatus, tvInfo; | |
private Button btnHttpRequest, btnReset; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
tvStatus = findViewById(R.id.tvStatus); | |
tvInfo = findViewById(R.id.tvInfo); | |
btnHttpRequest = findViewById(R.id.btnHttpRequest); | |
btnReset = findViewById(R.id.btnReset); | |
btnHttpRequest.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
getFirstTodo(); | |
} | |
}); | |
btnReset.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
tvStatus.setText("Press below button to fire HTTP request"); | |
tvInfo.setText("No data"); | |
} | |
}); | |
} | |
private void getFirstTodo() { | |
tvStatus.setText("HTTP Request in progress."); | |
apiService.getFirstTodo() | |
.observeOn(SchedulerProvider.ui()) | |
.subscribeOn(SchedulerProvider.io()) | |
.subscribe(new Consumer<Todo>() { | |
@Override | |
public void accept(Todo todo) throws Exception { | |
// Update the UI | |
tvStatus.setText("Request Completed! \nhttps://jsonplaceholder.typicode.com"); | |
tvInfo.setText(todo.getFormattedInfo()); | |
} | |
}, new Consumer<Throwable>() { | |
@Override | |
public void accept(Throwable throwable) throws Exception { | |
// Throw error | |
throwable.printStackTrace(); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment