I have an async task class that acts as a wrapper for http requests. I want to be able to either return the data from onPostExecute or somehow specify a method to send that data to after. I can't just call the function explicately because different responses have to go to different methods. Maybe if theres something like a call_user_function from php.
private class Get extends AsyncTask<Void, Void, ContainerData> {
@Override
protected void onPreExecute() {
if (pbTitle != null) {
pbTitle.setVisibility(View.VISIBLE);
}
if (preText != null) {
tvUpdate.setText(preText);
tvUpdate.setVisibility(View.VISIBLE);
}
}
@Override
protected void onProgressUpdate(Void... progress) {
}
//this line will not work, the return type wants to be void
@Override
protected ContainerData onPostExecute(ContainerData obj) {
if (pbTitle != null) {
pbTitle.setVisibility(View.GONE);
}
if (preText != null) {
tvUpdate.setText("");
tvUpdate.setVisibility(View.GONE);
}
updateResponse(obj);
return obj;
}
@Override
protected ContainerData doInBackground(Void... params) {
ContainerData obj = null;
RestClient client = new RestClient(context);
for (Param param : methodParams) {
client.AddParam(param.getKey(), param.getValue());
}
client.setMethod(method);
try {
client.Execute();
} catch (Exception e) {
e.printStackTrace();
}
obj = client.getResponseObject();
return obj;
}
public String updateResponse(ContainerData responseObject) {
try {
// throws API exception
responseObject.checkSuccess();
String message = responseObject.getMessage();
if (message != null) {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
dataString = responseObject.getDataString();
} catch (ApiException e) {
Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
return dataString;
}
}