Raditya Kurnianto
Raditya Kurnianto

Reputation: 744

Pass string parameter to asyn task

Hello I'm working with android android and doing some task in a async task. I want to call the async task and pass on string as a parameter. I've search google but with no luck, here is my code to call the async task

new warkah_detail().execute(id_warkah);

and here is my async task

class warkah_detail extends AsyncTask<String, String, String>
{
    @Override
    protected void onPreExecute()
    {
        pDialog = new ProgressDialog(warkah.this);
        pDialog.setMessage("Fetching Details...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... args)
    {
        try
        {
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("id_warkah", id_warkah));
            json = jParser.makeHttpRequest(url_warkah, "POST", params);

Those code raise an error like this Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

What is the best way to pass string to async task? I will appreciate any help. Thanks.

[UPDATE] I had removed the toast, but I still not receive the correct string that pass to async task from the called. Any idea?

Upvotes: 0

Views: 159

Answers (5)

Gleb
Gleb

Reputation: 445

Here is an example:

     public class DoSomethingTask extends AsyncTask<String, Void, String> {

           protected String doInBackground(String... params) {
                  // You call your parameter like this
                  String someStringResult = doSomething(params[0]);
                  return someStringResult;
           }

           protected void onPostExecute(String result) {
                  doSomethingWithResult(result);
           }
     }

And then you call it like this:

      new DoSomethingTask().execute("string parameter");

And by the way, read more about AsyncTask here: http://developer.android.com/reference/android/os/AsyncTask.html

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157447

Toast.makeText(getApplicationContext(), "ID : " + id_warkah, Toast.LENGTH_LONG).show();

Toast needs to be excuted on the UI Thread. Move it on the onPostExcuted or use runOnUiThread

your String is args[0]. You should read the documentation for varargs

Upvotes: 4

Raghunandan
Raghunandan

Reputation: 133560

As suggested by blackbelt you are updating ui on the background thread. Do it onPostExecute(). Or use runOnUiThread

      runOnUiThread(new Runnable() //run on ui thread
      {
           public void run() 
           { 
               // update ui     
           }
      });

To use the parameter use it like

       params.add(new BasicNameValuePair("id_warkah", args[0]));

You can check the docs for more info

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

You can also pass the parameter to the constructor of the asynctask and use the same.

Upvotes: 0

Rupesh Nerkar
Rupesh Nerkar

Reputation: 391

add new warkah_detail(id_warkah).execute(); instead of new warkah_detail().execute(id_warkah); and write a constructor for defining your parameter.

Upvotes: 0

Sagar Maiyad
Sagar Maiyad

Reputation: 12733

Just Remove Below line from your onBackground method.

Toast.makeText(getApplicationContext(), "ID : " + id_warkah, Toast.LENGTH_LONG).show();

and put it on onPostExecute method or use runOnUiThread.

Upvotes: 1

Related Questions