I am relatively new to Android programming, but I do come from a Java background, building back-end stuff for Enterprise applications. Now, I am working on an Android project in which I would like to retrieve a java.util.List of Restaurants from the server.
Currently, to achieve these tasks, I am using a simple Thread with a new Runnable, as the thread is immediately in Thread-pool and I can then simply join the Thread, and wait till it receives a reply from the server and then send it back. Very convenient this is and working good.
However, there are two things I cannot do with Threads, check for Internet-connection on the client-side and display a progress-bar. As far as I understood and read, these both things can be achieved by AsyncTask.
But here is the critical point, I don't want the AsyncTask to execute in some near future and then return the result, because with my current Thread model, I can simply join the Thread and relax.
Now, my question is, is the same possible in AsyncTask, I don't mean using get method after 1000msec. I meant actually waiting for it. How do I accomplish that? And with that, I want to know how do I check for INternet, and if successful, then only make a request.
Here is one of the Async methods I have in the project and then the next is the simple Thread model I have in the project.
private class LoginUserViaRest extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
final EditText userEmail = (EditText) findViewById(R.id.usernameText);
final EditText userPassword = (EditText) findViewById(R.id.PasswordField);
rest.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
StaticRestTemplate.jsessionid = rest.execute(StaticRestTemplate.baseURL+"j_spring_security_check", HttpMethod.POST,
new RequestCallback() {
@Override
public void doWithRequest(ClientHttpRequest request) throws IOException {
request.getBody().write(("j_username=" + userEmail.getText().toString() + "&j_password=" + userPassword.getText().toString()).getBytes());
}
}, new ResponseExtractor<String>() {
@Override
public String extractData(ClientHttpResponse response) throws IOException {
List<String> cookies = response.getHeaders().get("Cookie");
if (cookies == null) {
cookies = response.getHeaders().get("Set-Cookie");
}
String cookie = cookies.get(cookies.size() - 1);
int start = cookie.indexOf('=');
int end = cookie.indexOf(';');
return cookie.substring(start + 1, end);
}
});
return null;
}
@Override
protected void onPostExecute(String aVoid) {
super.onPostExecute(aVoid);
}
}
Thread to retrieve images :
@Override
public List<RestImage> getImagesForMenuCard(final int menuCardId) {
final RestTemplate restTemplate = StaticRestTemplate.getRest();
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("Cookie", "JSESSIONID=" + StaticRestTemplate.jsessionid);
requestHeaders.setAccept(Collections.singletonList(new MediaType("application", "json")));
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
menuCardImageEntity = restTemplate.exchange(menucardImageList + menuCardId, HttpMethod.GET, requestEntity, RestImage[].class);
}
});
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
RestImage[] restImages = menuCardImageEntity.getBody();
List<RestImage> restImageList = new ArrayList<>();
Collections.addAll(restImageList, restImages);
return restImageList;
}
I find the Thread model easy, as I know it better. I really don't understand the requirement to create a class to make a Network request. Is my understanding of AsyncTask broken or this is the way it is supposed to be.
Apologies for the copious amount of text, thought I would concisely explain my situation. Thanks a lot.