Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to let Async Task finish in Android

I'm working on an app that retrieves data from network, stores them to the device and then reads them. Problem is, I get my data in a Async Task.. And my app doesn't let the task finish before trying to show the data to the user.. I've tried task.get() but without result (it just stops there).

Here is my task:

public GetOptionsTask(XMLPortalGetOptions request) {
        super(request);
    }
    protected void onCancelled(){
        // TODO afficher message pas d'options sur le disque
    }
    @Override
    public void handleError(Transaction transaction) {
        // TODO afficher message pas d'options sur le disque
    }
    @Override
    public void handleSuccess(Transaction transaction) {
        saveOptions(transaction.getResponse());
        request = null;
        Log.d(OptionsManager.class.getName(), this.getStatus().toString());
    }

This task is an instance of my custom Async Task:

protected BaseXMLTransaction request;

public abstract void handleError(Transaction transaction);
public abstract void handleSuccess(Transaction transaction);
public TransactionTask(BaseXMLTransaction request){
    this.request = request;
}
@Override
protected Void doInBackground(Void... params) {
    try {
        Log.i(TransactionTask.class.getName(), "Doing in background");
        SocketHandler.sendTransaction(this, request.getRequest());
    } catch (SocketHandlerNotConfiguredException e) {
        Log.e(TransactionTask.class.getName(), "SocketHandler's parameters were not set.");
    }
    return null;
}
@Override
public void transactionResult(Transaction transaction) {
    switch (transaction.getCode()) {
        case ERROR:
            Log.e(TransactionTask.class.getName(), "ERROR !!!");
            handleError(transaction);
            break;
        case NO_CLIENT:
            Log.e(TransactionTask.class.getName(), "No Client Error");
            handleError(transaction);
            break;
        case NO_SERVER:
            Log.e(TransactionTask.class.getName(), "No Server Error");
            handleError(transaction);
            break;
        case OLD_VERSION:
            Log.e(TransactionTask.class.getName(), "Old Version");
            handleError(transaction);
            break;
        case TIMEOUT:
            Log.e(TransactionTask.class.getName(), "Transaction Timeout");
            handleError(transaction);
            break;
        case SUCCESS:
            Log.i(TransactionTask.class.getName(), "Transaction Success");
            handleSuccess(transaction);
    }
}

I seriously don't know what to do... Execute goes to fast and get doesn't do anything since I'm not returning anything I guess.

like image 300
dequec64 Avatar asked Nov 09 '22 21:11

dequec64


1 Answers

onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.

@Override
protected void onPostExecute(String result) {

}
like image 153
seba123neo Avatar answered Nov 14 '22 22:11

seba123neo