How to fetch data out of AsyncTask class in android? - java

I want to know how to access the data and bind it to a component in out of the AsyncTask class body?
I have a class like:
class DownloadData extends AsyncTask<String, Void, String> {....}
and it has a method :
#Override
protected String doInBackground(String... params) {
return ....;//return some data
}
I don't understand doInBackground return data to where?
Because when i want to use my class, i use it like:
DownloadData dd = new DownloadData();
dd.execute(...);
can i use it like this? because i want to fetch returned data out of my main class to bind it to some components
DownloadData dd = new DownloadData();
string temp=dd.doInBackground(...);

After doInBackground() your return will be forwarded to onPostExecute().
To use it in your activity refer this link : How to use Async result in UIThread

I can not catch
#Override
protected String doInBackground(String... params) {
return ....;//return some data
}
result from main UI.
You have to use callbacks. For example, you can use interface to obtain the result.
For example create an interface:
public interface IProgress {
public void onResult(int result);
}
create class:
private class DownloadData extends AsyncTask<String, Void, String> {
private IProgress cb;
DownloadData(IProgress progress) {
this.cb = cb;
}
#Override
protected String doInBackground(String... params) {
for (int i = 0; i < 10; i ++) {
if (cb!=nil)
cb.onResult(i);//calls 10 times
}
....
}
...
}
somewhere in code:
DownloadData dd = new DownloadData( new IProgress() {
public void onResult(int result) {
/// this is your callback
//to update mainUI thread use this:
final res = result;
runOnUiThread(new Runnable() {
#Override
public void run() {
//update UI here
textview.setText("" + res);
}
});
}
});
dd.execute(...);
And, as usual, you can update UI after doInBackground via onPostExecute()

If you just want to return a result from your AsyncTask class so that you can update the UI in your activity according to the result you can do something like this:
In your AsyncTask class declare an interface like this:
private AsyncResponse asyncResponse = null;
public DownloadData(AsyncResponse as) {
this.asyncResponse = as;
}
public interface AsyncResponse {
void onAsyncResponse(String result); // might be any argument length of any type
}
and in onPostExecute() :
asyncResponse.onAsyncResponse(result); // result calculated from doInBackground()
and in the activity class:
DownloadData dd = new DownloadData(new AsyncResponse() {...}); // implement onAsyncResponse here
dd.execute();

Related

assign external variable to the result of a callback inside AsynckTask

private class exampleTask extends AsyncTask<String, Void, SomeResult>{
#Override
protected SomeResult doInBackground(String... urls) {
SomeResult res ;
someMethod(new CallBack<T>(){
#Override
public void onResponse(SomeResult something) {
res = something ;
}
#Override
public void onFailure() {
//
}
});
return res ;
}
#Override
protected void onPostExecute() {
//
}
}
Please I want to assign "res" to "something" witch is inside the callback in the onResponse method. In this code it's impossible to assign res inside the onResponse method.
Please any help is welcome.
Thank you :)
my original code : I'd like to assign "url" ;
private class GetBeaconInfosTask extends AsyncTask<String, Void, Call<Url>> {
Url url ;
#Override
protected Call<Url> doInBackground(String... urls) {
ProxService service = ProxService.Factory.makeProxService(ProxService.ENDPOINT);
return service.getUrlDetails(urls[0]);
}
// onPostExecute return the results of the AsyncTask.
#Override
protected void onPostExecute(Call<Url> call) {
call.enqueue(new Callback<Url>() {
#Override
public void onResponse(Call<Url> call, Response<Url> response) {
url = response.body();
}
#Override
public void onFailure(Call<Url> call, Throwable t) {
//
}
});
if(url == null){
Log.i("url is null", "url is null !!!!! ....");
}
else {
setShopLogo(url.icon) ;
setShopName(url.title);
setDescription(url.description);
setlongUrl(url.longUrl); }
}
}
Assuming the problem is due to the variable having to be declared as final to be accessible from the callback, you could move it out of the doInBackground method and declare it as a member of the exampleTask class.
private class exampleTask extends AsyncTask<String, Void, SomeResult>{
SomeResult res;
#Override
protected SomeResult doInBackground(String... urls) {
someMethod(new CallBack<T>(){
#Override
public void onResponse(SomeResult something) {
res = something ;
}
#Override
public void onFailure() {
//
}
});
return res ;
}
#Override
protected void onPostExecute() {
//
}
}
While this should answer your question, it's hard to tell what you're trying to achieve. Further to #Gabe Sechan's answer - your variable may never get assigned, or it would be assigned after a certain length of time. Ordinarily you would carry out work in the doInBackground and when that has been carried out onPostExecute is called, but in your case you'll likely have onPostExecute called before hitting the onRespons of your callback. I don't think it's good practice to use a Callback in doInBackground the way you're trying to use it.
If you have other work that needs to be carried out asynchronously in doInBackground, do that, but move the someMethod call to onPostExecute and do whatever you need to do with res in there.
private class exampleTask extends AsyncTask<String, Void, SomeResult>{
SomeResult res;
#Override
protected SomeResult doInBackground(String... urls) {
// background work
}
#Override
protected void onPostExecute() {
someMethod(new CallBack<T>(){
#Override
public void onResponse(SomeResult something) {
res = something ;
// DO STUFF WITH RES HERE AFTER IT'S BEEN ASSIGNED OR YOU WON'T HAVE ACCESS TO IT
}
#Override
public void onFailure() {
//
}
});
}
}
EDIT: Now that the question contains real code, I can provide a flow that should work for you. Note in the code I pasted above the comment in onResponse stating this is where you should have the code that uses the variable. Look at onResponse below for example using your code. The value doesn't exist until you get to the onResponse callback method, so you have to wait until it gets there. Your code below is being carried out before onResponse so the variable is inevitably null. The code isn't executed sequentially. I'd suggest studying callbacks to fully understand them :)
private class GetBeaconInfosTask extends AsyncTask<String, Void, Call<Url>> {
Url url ;
#Override
protected Call<Url> doInBackground(String... urls) {
ProxService service = ProxService.Factory.makeProxService(ProxService.ENDPOINT);
return service.getUrlDetails(urls[0]);
}
// onPostExecute return the results of the AsyncTask.
#Override
protected void onPostExecute(Call<Url> call) {
call.enqueue(new Callback<Url>() {
#Override
public void onResponse(Call<Url> call, Response<Url> response) {
url = response.body();
setShopLogo(url.icon) ;
setShopName(url.title);
setDescription(url.description);
setlongUrl(url.longUrl); }
}
#Override
public void onFailure(Call<Url> call, Throwable t) {
//
}
});
}
You seem to not understand the purpose of a callback. A callback is called at some point in the future. Not now. So the function that sets up the callback won't be assured its called by the end of its execution. What you want will never work. Instead, you should execute whatever you wanted to do in onPostExecute with that value in the body of the callback.

Trying to get a value out of the OnPostExecute

i've got something blowing my mind all day long.
The question is, I have an AsyncTask that returns me an User Object, with all its attributes. I know that I have to return from the doInBackground method and receive in the OnPostExecute to work with my data. The fact is that i want to extract that user out of the AsyncTask method because i have to work with it in my main thread.
My AsyncTask class is placed in the MainActivity.class.
i've heard about using interfaces to get my value back but i can't understand the way to do it.
public class FetchUserDataAsyncTask extends AsyncTask<Void, Void, User> {
RequestHandler rh = new RequestHandler(); //this is the class i use to do de server conection
User user;
User ret_user;
public FetchUserDataAsyncTask(User user){
this.user = user;
}
#Override
protected void onPostExecute(User user) {
super.onPostExecute(user);
//I WANT THIS USER IN MY MAIN THREAD, TO WORK WITH ITS ATTRIBUTES
}
#Override
protected User doInBackground(Void... params) {
try {
HashMap<String, String> dataToSend = new HashMap<>();
dataToSend.put("username", user.username);
dataToSend.put("password", user.password);
ret_user = rh.sendGetRequest("myadresstophp.php", dataToSend);
} catch (Exception e) {
e.printStackTrace();
}
return ret_user;
}
and the call (when user press the log in button), a few lines above.
new FetchUserDataAsyncTask(userk).execute();
I was hoping to do something like that: (i know its not the way to do it)
User user = new FetchUserDataAsyncTask(userk).execute();
Thank you all, have a nice day!
At first declare an Interface in your project somewhere having the required functions, then implement that interface in the (AsyncTask)calling class ,then declare one Interface object in the AsyncTask. Create the constructor of AsyncTask as follows:
public FetchUserDataAsyncTask(User user,InterfaceClass object){
this.user = user;
this.interfaceObject=object;
}
And then do the following in onPostExecute:
#Override
protected void onPostExecute(User user) {
super.onPostExecute(user);
interfaceObject.function(user); //call the function of the calling class
}
You can create an interface, pass it toAsyncTask (in constructor), and then call method in onPostExecute()
For example:
Your interface:
public interface OnTaskCompleted{
void onTaskCompleted();
}
Your Activity:
public class YourActivity implements OnTaskCompleted{
// your Activity
}
And your AsyncTask:
public class YourTask extends AsyncTask<Object,Object,Object>{
//change Object to required type
private OnTaskCompleted listener;
public YourTask(OnTaskCompleted
listener){
this.listener=listener;
} // required methods
protected void onPostExecute(Object
o){
// your stuff
listener.onTaskCompleted();
}
}
Here you go, this is in general how it would work. This is based on this answer and modified to be specific to your existing code. Basically assign your member variable based on what the user entered, and compare that value to the one you get from the server:
public class MyClass extends Activity {
//member variable for the username the user enters:
User userEnteredUser;
public class FetchUserDataAsyncTask extends AsyncTask<Void, Void, User> {
RequestHandler rh = new RequestHandler(); //this is the class i use to do de server conection
User user;
User ret_user;
public FetchUserDataAsyncTask(User user){
this.user = user;
}
#Override
protected void onPostExecute(User user) {
super.onPostExecute(user);
//I WANT THIS USER IN MY MAIN THREAD, TO WORK WITH ITS ATTRIBUTES
processValue(user); //added
}
#Override
protected User doInBackground(Void... params) {
try {
HashMap<String, String> dataToSend = new HashMap<>();
dataToSend.put("username", user.username);
dataToSend.put("password", user.password);
ret_user = rh.sendGetRequest("myadresstophp.php", dataToSend);
} catch (Exception e) {
e.printStackTrace();
}
return ret_user;
}
}
private void getValue()
{
EditText et = (EditText) findViewById(R.id.username);
userEnteredUser.username = et.getText().toString(); //something like this.... replace with your syntax
new FetchUserDataAsyncTask(userEnteredUser).execute();
}
private void processValue(User userFromServer)
{
if (userEnteredUser.equals(userFromServer)) {
//Users match! Yay!
}
}
}
onPostExecute runs on the UI thread by default (main thread). Do what ever you need to be do on the main thread in onPostExecute
I suggest you read up more from the link provided below for a better understanding on AsyncTask
http://developer.android.com/reference/android/os/AsyncTask.html
I'm uncertain what you mean by "main thread". If you mean "UI Thread", which is the primary thread used to execute in your app, then Shashank's reply is correct.
However, I suspect from the context of the question that you actually mean that you want the results returned to the code that initiated the AsyncTask. Let's call that the "invoking object" In that case, what I would do is define a callback in your invoking object whose only purpose is to receive the result of this AsyncTank. You could call it onUserDataFetched(User user).
So, to do this using Interfaces, you could define an Interface that contains a single method:
public interface FetchUserDataListener {
public void onUserDataFetched(User user);
}
Then make sure that your InvokingObject implements that interface, and pass your InvokingObject (which implements FetchUserData, and thus can be considered that object type) to your AsyncTask when you instantiate it.
So, you invoking object code would look like this:
public class InvokingObject implements FetchUserData {
public void someMethodToInvokeFetchUserData() {
//....
new FetchUserDataAsyncTask(userk, this).execute();
//....
}
#Override
public void onUserDataFetched(User user) {
//Compare the result to the data in your invoking object
}
}
Then pass that callback to your AsyncTask when you construct it:
And your AsyncTask Code would look like this:
public class FetchUserDataAsyncTask extends AsyncTask<Void, Void, User> {
FetchUserDataListener mFetchUserDataListener;
public FetchUserDataAsyncTask(User user, FetchUserDataListener listner){
this.user = user;
mFetchUserDataListener = listener
}
//...
#Override
protected void onPostExecute(User user) {
super.onPostExecute(user);
listener.onUserDataFetched(user)
}
//...
}

Pass an object to AsyncTask

I have a list of objects with certain states
private ArrayList<MyObjectMap> MyList;
MyList = new ArrayList<>();
Every object in that list contains a certain value, that can be updated via the internet (say active, inactive). To update each element I use an AsyncTask, so something like this
for(int i=0;i<MyList.size();i++) {
new myAsyncTask(MyList.get(i)).execute();
}
Later, to update the List in my GUI, I use notifyDataSetChanged for the BaseAdapter of my list.
Is this somehow possible? How do I need to change my AsyncTask?
public class myAsyncTask extends AsyncTask<Void, Void, Void> {
private MyObjectMap myObject;
protected void onPreExecute() {
}
public myAsyncTask(MyObjectMap mom) {
myObject = mom;
}
#Override
protected Void doInBackground(Void... voids) {
myObject.updateThisItem();
return null;
}
}
Pass the full arrayList and baseAdapter object to the asyncTask. and update the current element of arrayList and then make notifyDatasetchanged()
public class MyAsyncTask extends AsyncTask<Void, Void, Void>
{
private ArrayList<MyObjectMap> myList;
private BaseAdapter baseAdapter;
private int position;
public MyAsyncTask(ArrayList myList, int position, BaseAdapter baseAdapter) {
this.myList = myList;
this.position = position;
this.baseAdapter = baseAdapter;
}
protected void onPreExecute()
{
}
#Override
protected Void doInBackground(Void... voids)
{
myList.get(position).updateThisItem(); //or
/*
MyObjectMap mop = myList.get(position);
mop.updateThisItem();
myList.remove(position);
myList.add(position, mop);
*/
return null;
}
protected void onPostExecute()
{
baseAdapter.notifyDataSetChanged();
}
}
you can update ui components in asyncTask in both onPreExecute and onPostExcute...
#Prassana has already shown you how to pass objects to AsyncTask by using a constructor, but there is another elegant way to do it, by altering the AsyncTask class type paramers. If I wanted to pass an ArrayList< String> to AsyncTask, and receive an ArrayList< Integer> back, the class layout would look something like this.
// notice the return type and parameter type
public class myAsyncTask extends AsyncTask <ArrayList<String>, Void, ArrayList<Integer> {
...
// Make sure this method receives and returns the correct types.
// the params are specified when you make the call to execute the asynctask and
// are accessed in a usual varargs way (like an array).
#Override
public ArrayList<Integer> doInBackground(ArrayList<String>... params) {
...
// onPostExecute takes the ArrayList returned by doInBackground
#Override
public void onPostExecute(ArrayList<Integer> result) {
// do something with your newly acquired ArrayList<Integer>
Note that this is the main structure, just to give you an idea. I cannot garauntee that this is typo-less code. Good luck!
public class myAsyncTaskextends AsyncTask<Object, Void, Void> {
private MyObjectMap myObject;
protected void onPreExecute() {
}
#Override
protected Void doInBackground(Object... voids) {
myObject = (MyObjectMap)param[0];
myObject.updateThisItem();
return null;
}
}
when we execute asyn task use this code to send object
Asyntask.execute(loginRequestBean);
inside background method you can get object like this
#Override
protected Object doInBackground(Object... params) {
Object responseObject = null;
try {
Object object = params[0];
if (object instanceof LoginRequestBean) {
//login webservice
} else if (object instanceof RegisterBean) {
//registration webservice
}
}

AsyncTask must be abstract

I'm pretty new to AsyncTask on Android, I'm trying to pass an int with
new startTestAsync().execute(grayVal);
to this AsyncTask
public class startTestAsync extends AsyncTask<Integer, Void, Void> {
double[] timeArray = new double[500]; // set array
double lagStartTime;
double lagEndTime;
double lagTimeResult;
int testAmount;
protected void doInBackground(int...grayVal) {
for (testAmount = 0; testAmount < 500; testAmount++) {
lagStartTime = System.nanoTime(); //start lagTimer start
while (grayVal >= 100) {
Log.i("Mat Value", String.valueOf(grayVal));
lagEndTime = System.nanoTime();
}
}
}
Android studio says that "AsyncTask must be abstract", I have no idea how to fix this as I am new to AsyncTasks.
edit: full code http://pastebin.com/vrf8Rb3h
From the documentation:
AsyncTask must be subclassed to be used. The subclass will override at
least one method (doInBackground(Params...)), and most often will
override a second one (onPostExecute(Result).)
That means, you have to override the method onPostExecute(Result) too:
protected void onPostExecute(Long result) {
//Do something
}
you can pass parameters to AsyncTask with two approach.
these are:
1-make parameterized constructor.means
You can override the constructor. Something like:
private class MyAsyncTask extends AsyncTask<Void, Void, Void> {
public MyAsyncTask(int showLoading) {
super();
// do stuff
}
// doInBackground() et al.
}
and call this AsynTask like
MyAsyncTask task = new MyAsyncTask();
task.showLoading = 12;
task.execute();
2-pass values in execute() statement.
public class MyAsyncTask extends AsyncTask<MyClass, Void, Boolean> {
#Override
protected Boolean doInBackground(MyClass... params) {
// Do blah blah with param1 and param2
MyClass myClass = params[0];
String param1 = myClass.getParam1();
String param2 = myClass.getParam2();
return null;
}
}
and call it like
AsyncTask asyncTask = new MyAsyncTask().execute(new MyClass());
enjoy your code:)
You should be doing it like this:
protected Void doInBackground(Integer... grayVal)
{
for (testAmount = 0; testAmount < 500; testAmount++)
{
lagStartTime = System.nanoTime();
while (grayVal[0] >= 100) // you need to loop over grayVal properly if you are sending multiple values(it's just an example), if there is just one value you can write grayVal[0].
{
Log.i("Mat Value", String.valueOf(grayVal.intValue()));
lagEndTime = System.nanoTime();
}
}
return null;
}
you set the parameters of doInBackground to int, it should be an Integer and the return is a Void rather than a void so you should explicitly return null.

public AsyncTask to be called from several classes

I'm new in Android. It's been a few weeks since I started working in this project. Reading the code already made I see a lot of private async tasks doing basically the same (call an API) So I wonder if there is a better solution, my idea would be create a public async task called ApiCaller which will return a JSONObject and the responsible for parsing the JSON would be the class calling the ApiCaller:
public class ApiCaller extends AsyncTask<String, String, JSONObject> {
private static final String TAG = "ApiCall";
private final String apiVersion = "v1";
private final String baseURL = "http://my.api.com/";
private String URL = null;
/**
* Generates the URL to call the API.
*
* #param params List with the params to call the API.
*/
public ApiCaller(ArrayList<NameValuePair> params){
String apiURL = this.baseURL + this.apiVersion + "/?";
String paramsList = URLEncodedUtils.format(params, "utf-8");
this.URL = apiURL + paramsList;
}
#Override
protected JSONObject doInBackground(String ... params) {
Log.i(TAG, "API:");
Log.i(TAG, this.URL);
JSONManager jParser = new JSONManager();
JSONObject jsonObject = jParser.getJSONFromUrl(this.URL);
return jsonObject;
}
Is there a way to return that JSONObject outside of the class so I can do something like:
JSONObject js = apiCaller.execute();
Or any other solution to avoid creating new asynctasks every time I need to call the API? With my current code I can't get it but I don't know what is missing? maybe returning it in onPostExecute?
Some time ago I've asked a similar question
One AsyncTask for multiple Activities
and the solution I found was answered in another question:
Common class for AsyncTask in Android?
Basically, what you need is an interface.
I've going to explain the basics, although you should check the original answer by #SirDarius.
You could create an interface like this:
interface AsyncTaskCompleteListener<T> {
public void onTaskComplete(T result);
}
And implements that interface in all classes you need to use the AsynTask, then, in your generic Asynstask you need to have a callback AsyncTaskCompleteListener and call it from your onPostExecute
class B implements AsyncTaskCompleteListener<JSONObject> {
public void onTaskComplete(JSONObject result) {
// do whatever you need
}
public void launchTask(String url) {
ApiCaller a = new ApiCaller(context, ArrayList<NameValuePair> params, this);
ApiCaller.execute(url);
}
}
class ApiCaller extends AsyncTask<String, Void, String> {
private AsyncTaskCompleteListener<String> callback;
public ApiCaller(Context context, ArrayList<NameValuePair> params, AsyncTaskCompleteListener<String> cb) {
this.context = context;
this.callback = cb;
}
protected void onPostExecute(String result) {
finalResult = result;
progressDialog.dismiss();
System.out.println("on Post execute called");
callback.onTaskComplete(result);
}
}
because the asynchronous AsyncTask you can not call a method and obtain the return value;
you could use a AsyncTask on your inline code and get on postExecute your JSON object:
new AsyncTask<String, String, JSONObject>(){
#Override
protected Void doInBackground(String... params) {
//...
}
#Override
protected void onPostExecute(JSONObject result) {
// ...
}
}.execute(...);
You should implement a listener that you will pass to the async task. When the asynctask finishes, it will call the listener's method:
First, make a listener in a new java file:
public interface onTaskDoneListener{
void onTaskDone(JSONObject result);
}
Pass it to your AsyncTask:
private onTaskDoneListner donelistner;
public ApiCaller(ArrayList<NameValuePair> params,onTaskDoneListener donelistener){
String apiURL = this.baseURL + this.apiVersion + "/?";
String paramsList = URLEncodedUtils.format(params, "utf-8");
this.URL = apiURL + paramsList;
this.donelistener = donelistener;
}
Then, inside the onPostExecute method of your AsyncTask
this.donelistener.onTaskDone(result)
Usage:
new ApiCaller(params,new onTaskDoneListener() {
#Override
public void onTaskDone(JSONObject result) {
//This will be called when the asynctask finishes
//Do something with the result
});
Step 1 : Define an Interface.
public interface WebServiceListener {
public void onLoginActionComplete (ArrayList<String> arrayList);
}
Step 2: Implement the WebServiceListener in your activity class.
public class LoginActivity extends Activity implements WebServiceListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
/* Calling the AsyncTask here,you could call in Login Button Click... */
WebServiceClient httpclient = new WebServiceClient(this);
httpclient.execute(ssoURLStr);
}
#Override
public void onLoginActionComplete (ArrayList<String> arrayList) {
// TODO Auto-generated method stub
/* Read the response */
String Response = arrayList.get(0);
}
}
Step 3 : Your AsynTask class code here.
public class WebServiceClient extends
AsyncTask<String, Integer, ArrayList<String>> {
WebServiceListener listener = null;
/* Constructor added WebServiceListener here */
public WebServiceClient ( WebServiceListener listener)
{
this.listener = listener;
}
#Override
protected ArrayList<String> doInBackground(String... params) {
// TODO Auto-generated method stub
ArrayList<String> arrayList = null;
" write your http code here and get the response and update the
arrayList <String> here"
return arrayList;
}
#Override
protected void onPostExecute(ArrayList<String> arrayList) {
// Returns the contents of the HTML page
listener.onLoginActionComplete (arrayList);
}
}
I have also asked this question. Maybe this link will help

Categories

Resources