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