There is really no need to use a Bundle here, you can just pass in the Strings to the varargs
that doInBackground()
takes.
You would just call it like this, where json
is a JSONObject:
asyn.execute(url, json.toString());
Then, you would make doInBackground()
take String varargs
:
class Asyn extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... arg0) {
String url = arg0[0];
String json = arg0[1];
JSONObject obj = new JSONObject(json);
}
}
If you really want to use a Bundle, you could call it as you are now:
Bundle bundle = new Bundle();
bundle.putString("url", url);
bundle.putString("json",json.toString());
//inte.putExtras(bundle); //not needed
asyn.execute(bundle);
Then make doInBackground()
take Bundle varargs
:
class Asyn extends AsyncTask<Bundle, Void, Void> {
@Override
protected Void doInBackground(Bundle... arg0) {
Bundle b = arg0[0];
String url = b.getString("url");
String json = b.getString("json");
JSONObject obj = new JSONObject(json);
}
}
In addition, you should not be trying to capture a return value from the AsyncTask
. If the AsyncTask
is a subclass of the Activity, then you could just set the value in onPostExecute()
, and call a method in your enclosing Activity.
For other ways to do it, see this post.