This question already has answers here:
AsyncTask and error handling on Android
(12 answers)
Closed 9 years ago.
Currently I'm working on implementing network communication layer. And I thought it's good to consult with more experienced Android developers first.
I have a class called WebApiController which is responsible for: making requests and parsing responses, and storing them in the models. WebApiController methods execute on the main thread, so I wrap them in AsyncTasks to take the load out of the main thread. WebApiController methods potentially throws exceptions such as ServerBadResponseException, XmlParserException, etc. And I want to be able to handle them accordingly to the type of error (i.e. show different error messages based on the type of error). So, what would be the best way to notify the onPostExecute about the error type, and nicely handle it there.
Or this:
class SomeTask extends AsyncTask<Void, Void, Params> {
#Override
protected Params doInBackground(Void... voids) {
Params params = new Params();
try {
.....
params._result = .....
} catch (Throwable e) {
params._error = e;
}
return params;
}
#Override
protected void onPostExecute(Params params) {
if(params._error != null){
....
} else {
....
}
}
}
class Params {
public Throwable _error;
public Object _result;
}
Basically the easiest way is to set a return code like "SUCCESS", "FAILURE_XY" and process that in your onPostExecute(). This works also, when you normally would return data...
doInBackground() {
try {
// code
return data;
} catch (Exception e) {
Log.e(TAG, "error description", e);
resultCode = FAILURE;
return null;
}
}
onPostExecute(Data data) {
if (data == null) {
// check resultCode!
} else {
// work with your data
}
}
You can try this (in doInBackgroung method):
try {
........
} catch (final Throwable e) {
runOnUiThread(new Runnable() {
public void run() {
Log.e(TAG, e.getMessage(), e);
Toast.makeText(......).show();
}
});
}
Related
I am using AsyncTask to get the data form a web service that I created. Now I want to display proper error messages to user like if the Internet is not available it will display toast for that similarly if server is down it will display toast for that. I want to set a string with error like "server is down" or "internet problem occured" in doInBackground() and display Toast in onPostExecute() but I want to know if my server is down that what exception is thrown? and if my server is active but during transfer internet is disconnected so what exception is thrown ?
Probably modelling the response is the best and easiest way.
For example,
you can compose a model from the data you got such as:
class ApiResponse {
public final String responseString;
public final Throwable error;
public ApiResponse (String responseString,Throwable error){
this.responseString = responseString;
this.error = error;
}
}
Then, you can bind response or error to that model and return from doInBackground();
Pseudo code :
class ApiAsyncTask extends AsyncTask<Void, Void, ApiResponse> {
....
protected ApiResponse doInBackground() {
try {
//callApi and get response, if success pass null in error
return new ApiResponse(responseString, null)
} catch (Exception e) {
e.printStackTrace();
//otherwise pass that error
return new ApiResponse(null, e);
}
}
protected void onPostExecute(ApiResponse apiResponse) {
//now just need to check if error is null
if (error == null) {
String json = apiResponse.responseString;
//parse response
} else {
//get error and check with instanceOf
Throwable error = apiResponse.error;
if (error instanceOf SocketTimeoutException){
//show timeout error
}
else if (error instanceOf SomeXYZException){
//handle that exception
}
}
}
}
This is just an example. You can put anything you want in ApiResponse and compose model of that data. (such as status code got from api, generated pojo class of json response by some converter etc. anything). Once you have the data bounded, you can use it in onPostExecute() as it will always be running on your UI thread. Note that Third type param Result of AsyncTask is built for that by definition : AsyncTask<Params, Progress, Result>.
Catch exceptions of your doInBackground() to string and than, depends what you need, you can show toast message from onPostExecute() method, something like this:
#Override
protected Void doInBackground(Void... arg0) {
try {
// do you stuff here
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "UnknownHostException: " + e.toString();
} catch (IOException e) {
e.printStackTrace();
response = "IOException: " + e.toString();
} finally {
...
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
protected void onPostExecute(Void result) {
if (response.contains("IOException: java.net.ConnectException:")) {
Toast.makeText(getApplicationContext(), "YOUR TEXT HERE", Toast.LENGTH_SHORT).show();
mProgressDialog.dismiss();
}
else if (response.contains("IOException: java.net.SocketTimeoutException:")) {
Toast.makeText(getApplicationContext(), "YOUR TEXT HERE", Toast.LENGTH_SHORT).show();
mProgressDialog.dismiss();
}
else if (response.contains("IOException: java.net.SocketException:")) {
Toast.makeText(getApplicationContext(), "YOUR TEXT HERE", Toast.LENGTH_SHORT).show();
mProgressDialog.dismiss();
}
}
Of course, this is yust an example, but you could see how that can be worked out.
I am writing here because this is my last solution of understanding this type of programming.The problem is that I got stuck on what to use to handle the connection to a server and log-in. Should I use async task, handler or thread ? I didn't find a concrete answer stating which one to use, only found that async task is used to download images or other download stuffs.
Until now I have used a thread to connect to the server. The problem I encountered was when I catch the exception ( Putting invalid username/password ) and try to log-in again. ( I needed to "close" the last thread and start one again )
After this I started to use async task but I don't really understand how it should work and I am stuck on a toast of invalid username/password.
private class connectStorage extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
try {
api = DefaultClientFactory.create(host, getUser, getPassword);
if (api.getAuthToken().trim().length() > 3) {
//TO DO LAYOUT CHANGE;
}
} catch (StorageApiException e) {
e.printStackTrace();
Log.i("TEST", "" + e.getMessage());
}
return null;
}
Also, I am 100% sure that calling inflate in the doInBackground method won't work too ( there I wanted to change the activity ).
I am starting the async task on a button press.
When you are using asynctask
You have doInBackground and onPostExecute
So basically get a json or string or boolean as a result from doinbackground
and in onpostexecute check if the login in succesful or not if its succesful save the data from server and start an intent to go to another activity or toast the user that that user login details are wrong and try again.
So your asynctask can be an inner class of your activity class which is login and onClickSubmit button call the asynctask class and on post execute parse the json and according to the result decide what to do
Example:
public class SignInAsycTask extends AsyncTask<RequestParams, Void, String> {
#Override
protected String doInBackground(RequestParams... params) {
return new HttpManager().sendUserData(params[0]);
}
#Override
protected void onPostExecute(String result) {
String[] details = parseJsonObject(result);
if (details != null) {
user.setUser_id(Integer.valueOf(details[0]));
user.setName(details[1]);
if (details.length > 2) {
user.setProfilePic(details[2]);
}
setSharedPreferences();
startActivity(new Intent(Signin.this, MainActivity.class));
finish();
} else {
Toast.makeText(Signin.this, "please try again",
Toast.LENGTH_LONG).show();
}
}
}
public String[] parseJsonObject(String result) {
JSONObject obj = null;
try {
obj = new JSONObject(result);
if (obj.has("success")) {
if (obj.getInt("success") == 1) {
if (obj.has("user_pic")) {
return new String[] {
String.valueOf(obj.getInt("user_id")),
obj.getString("user_name"),
obj.getString("user_pic") };
} else {
return new String[] {
String.valueOf(obj.getInt("user_id")),
obj.getString("user_name"), };
}
} else {
return null;
}
} else {
return null;
}
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
here my RequestParams are just a object where I stored all the details like url parameters to send etc and the output of the doinbackground is a String and I am parsing it in my postexecute method
Goodmorning,
I have a button on my android app that launches a search on the web (through google endpoints) through an AsyncTask. My problem is that the button does not "unclick" until the AsyncTask is completed, which may take several seconds. When the internet connection is slow, this even makes the application crash, in any case the application is completely stuck until the AsyncTask is completed. Now the reason for using AsyncTask was exactly to eliminate this problem, so I don't really get what happens!
Here is the OnClickListener:
SearchListener = new OnClickListener() {
#Override
public void onClick(View v) {
String cname=TextCourse.getText().toString();
if (!cname.isEmpty()){
try {
CollectionResponseWine listavini= new QueryWinesTask(messageEndpoint,cname,5).execute().get();
} catch (InterruptedException e) {
showDialog("Errore ricerca");
e.printStackTrace();
} catch (ExecutionException e) {
showDialog("Errore ricerca");
e.printStackTrace();
}
} else{
showDialog("Inserisci un piatto");
}
}
};
and here is the AsyncTask that is being called:
private class QueryWinesTask
extends AsyncTask<Void, Void, CollectionResponseWine> {
Exception exceptionThrown = null;
MessageEndpoint messageEndpoint;
String cname;
Integer limit;
public QueryWinesTask(MessageEndpoint messageEndpoint, String cname, Integer limit) {
this.messageEndpoint = messageEndpoint;
this.cname=cname;
this.limit=limit;
}
#Override
protected CollectionResponseWine doInBackground(Void... params) {
try {
CollectionResponseWine wines = messageEndpoint.listwines().setCoursename(cname).setLimit(limit).execute();
return wines;
} catch (IOException e) {
exceptionThrown = e;
return null;
//Handle exception in PostExecute
}
}
protected void onPostExecute(CollectionResponseWine wines) {
// Check if exception was thrown
if (exceptionThrown != null) {
Log.e(RegisterActivity.class.getName(),
"Exception when listing Messages", exceptionThrown);
showDialog("Non ci sono vini associati al tuo piatto. Aggiungine uno!");
}
else {
messageView.setText("Vini piu' votati per " +
cname + ":\n\n");
for(Wine wine : wines.getItems()) {
messageView.append(wine.getName() + " (" + wine.getScore() + ")\n");
}
}
}
}
...execute().get() is blocking. It makes UI thread wait for Task to complete.
Don't do get(). Use onPostExecute() to get the result (wines) of task and update the UI.
I am making an android app which requires it to fetch some information from a remote server and therefore i have to make a http request in a async task.Now the problem is that that the response sometimes take more than 2 secs and when it does it give http timeout exception but most of the time it works just fine .So i want to implement the functionality that when i recieve a http timeout exception i want to retry the request again(try the doinBackground again,because network call can only be made in thread other than the main thread) because chances are that it will be successful and all the things that need to be fetched from the remote server will occur in CallRemoteServer() method
Now in my program i have implemented something like this
new AsyncTask<Void, Void, Void>() {
private boolean httpResponseOK = true;
#Override
protected Void doInBackground(Void... params) {
try {
CallRemoteServer();
}
} catch (Exception e) {
httpResponseOK = false;
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
if (httpResponseOK == false) {
//Show an alert dialog stating that unable to coonect
}
else
{
//update UI with the information fetched
}
});
Can someone advice me how can i implement something which i have mentioned above ,i mean that if i get some other exception other than timeout than show an alert dialog otherwise retry atleast five time more CallRemoteServer method before showing the dialog that unable to connect.
I am not able to think of any good way to implement this logic.
Thanks in advance
You're probably getting a ConnectTimeoutException (or check in the logs what is the IOException you're getting). I would first try to extend the timeout. Some similar answers for this can be found here or here.
However, an auto-reconnect mechanism is a must to have. I would implement it using recursive code:
final int maxAttempts = 5;
protected MyServerData callRemoteServer(int attempt) throws IOException {
try {
// do the IO stuff and in case of success return some data
} catch (ConnectTimeoutException ex) {
if(attempt == maxAttempts) {
return callRemoteServer(attempt + 1);
} else {
throw ex;
}
}
}
Your doInBackground method should look like:
#Override
protected Void doInBackground(Void... params) {
try {
callRemoteServer(0);
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
In this way if the connection timeouts it will attempt to retry for 5 max times (you can set the max attempts to anything you like). Just make sure to return some data from this IO operation as that is the most valuable asset from that method anyway ...
For this reason I would change it to following:
private class MyAsynckTask extends AsyncTask<Void, Void, MyServerData> {
#Override
protected MyServerData doInBackground(Void... params) {
try {
return callRemoteServer(0);
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(MyServerData result) {
if(result != null) {
// display data on UI
}
}
}
I'm executing the practically identical code (besides the return statement) in onCreate() in two different ways:
once in a Thread (that works)
and once in an AsyncTask
Somehow the Exception thrown in the AsyncTask can't even be debugged... do you have an idea why this code doesn't work in the AsyncTask and why I can't debug de Exception? (It is not present as a variable)
public class TrainingProgressActivity extends ActivityTemplate {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_training_progress);
// the not working AsyncTask variant
new ProgressGetterTask().execute("");
// the working Thread variant
new Thread(new Runnable() {
public void run() {
try {
RestTemplate restTemplate = RestTemplateFactory.createRestTemplate();
FeatureSetCollectionProgress i = restTemplate.postForObject(URLHelper.BASE_PATH+"training/progress", getDevice(), FeatureSetCollectionProgress.class);
System.out.println(i.getMobilityProgress().get(0).getTransportationMeanType().toString());
} catch(Exception e) {
System.out.println("ex");
e.printStackTrace();
}
}}).start();
}
/**
* ProgressGetterTask. (not working ???)
*/
private class ProgressGetterTask extends AsyncTask<String, FeatureSetCollectionProgress, FeatureSetCollectionProgress> {
protected FeatureSetCollectionProgress doInBackground(String... urls) {
try {
RestTemplate restTemplate = RestTemplateFactory.createRestTemplate();
return restTemplate.postForObject(URLHelper.BASE_PATH+"training/progress", getDevice(), FeatureSetCollectionProgress.class);
} catch(Exception e) {
e.printStackTrace();
return null; // setting breakpoint here, 'e' cannot be resolved to a variable.
}
}
protected void onPostExecute(FeatureSetCollectionProgress featureSetCollectionProgress) {
if(featureSetCollectionProgress!=null) {
} else {
}
}
}
}