Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Asynctask passing a single string

I would like to pass a single string into an asynctask. Could anyone show me how it is done? my getEntity needs The method getEntity(Activity, String, EntityGetListener) but I keep passing this String[]

String pass= story.get(position).getEntity();

        new RemoteDataTask().execute(pass);





private class RemoteDataTask extends AsyncTask<String, String, Long> {

    @Override
    protected Long doInBackground(String... params) {
        // TODO Auto-generated method stub
        EntityUtils.getEntity(activity, params, new EntityGetListener() {
            @Override
            public void onGet(Entity entity) {

                viewcount = entity.getEntityStats().getViews();
            }

            @Override
            public void onError(SocializeException error) {

            }
        });
        return null;
    }

}
like image 213
KC Chai Avatar asked Jul 09 '13 13:07

KC Chai


People also ask

Why AsyncTask deprecated?

This class was deprecated in API level 30. AsyncTask was intended to enable proper and easy use of the UI thread. However, the most common use case was for integrating into UI, and that would cause Context leaks, missed callbacks, or crashes on configuration changes.

What is AsyncTask in Android with Example?

Android AsyncTask is an abstract class provided by Android which gives us the liberty to perform heavy tasks in the background and keep the UI thread light thus making the application more responsive. Android application runs on a single thread when launched.

How to call AsyncTask in Android?

To start an AsyncTask the following snippet must be present in the MainActivity class : MyTask myTask = new MyTask(); myTask. execute(); In the above snippet we've used a sample classname that extends AsyncTask and execute method is used to start the background thread.

What is the use of AsyncTask class?

In Android, AsyncTask (Asynchronous Task) allows us to run the instruction in the background and then synchronize again with our main thread. This class will override at least one method i.e doInBackground(Params) and most often will override second method onPostExecute(Result).


2 Answers

You already have this

     new RemoteDataTask().execute(pass); // assuming pass is a string

In doInbackground

     @Override
     protected Long doInBackground(String... params) {   

             String s = params[0]; // here's youre string
             ...      //rest of the code. 
     }

You can find more info @

http://developer.android.com/reference/android/os/AsyncTask.html

Update

Asynctask is depecated. Should be using kotlin coroutines or rxjava or any other threading mechanism as alternatives.

like image 177
Raghunandan Avatar answered Oct 10 '22 20:10

Raghunandan


You can build AsyncTask with a constructor.

public class RemoteDataTask extends AsyncTask<String, String, Long> {

    private String data;

    public RemoteDataTask(String passedData) {
        data = passedData;
    }

    @Override
    protected String doInBackground(Context... params) {
        // you can access "data" variable here.
        EntityUtils.getEntity(activity, params, new EntityGetListener() {
            @Override
            public void onGet(Entity entity) {
                viewcount = entity.getEntityStats().getViews();
            }
            @Override
            public void onError(SocializeException error) {
            }
        });
        return null;
    }
}

In the application (Activity, Service etc), you can use;

private RemoteDataTask mTask;
private void doStuff(){
    String pass = "meow"; // story.get(position).getEntity();
    mTask = new RemoteDataTask(pass);
    mTask.execute();
}
like image 44
az3 Avatar answered Oct 10 '22 20:10

az3