Skip to content

Instantly share code, notes, and snippets.

@mayuroks
Last active August 15, 2018 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mayuroks/5c9ddbfebbef609c6a171657327039bc to your computer and use it in GitHub Desktop.
Save mayuroks/5c9ddbfebbef609c6a171657327039bc to your computer and use it in GitHub Desktop.
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