Android AsyncTask helper class - android

I currently have a class UserFunctions that does all my user actions e.g. register, login etc. In the UserFunctions class there's a JSONParser object that does all the actual HTTP calls and returns the JSONObject.
public class UserFunctions {
private JSONParser jsonParser;
private static String registerURL = Constants.registerUrl;
// constructor
public UserFunctions(){
jsonParser = new JSONParser();
}
public JSONObject register(){
// getting JSON Object
JSONObject json = jsonParser.getJSONFromUrl(registerURL);
// return json
return json;
}
...
}
and then in my event handler of my activity classes I just do UserFunctions.register().
My question is, I now want to do all these calls in a background thread and at the same time show a ProgressDialog. I know running in the background thread is achieved with the AsyncTask.
But how should I achieve this design such that I can still do UserFunctions.register() in my Activity class, everything to be done in a background thread and a progressDialog shown.

Ok for this there are two parts the progress dialog and the async task, you need to move your JSONparser into the actual async task. If you want to use multiple progress dialogs just call them before calling the asynctask and close them when it returns
private class JsonRetriever extends AsyncTask<Url, Void, JSONObject>{
private JSONParser jsonParser;
private ProgressDialog dialog;
public JsonRetriever(Context c){
dialog= new ProgressDialog(c);
jsonParser= new JSONParser();
}
protected void onPreExecute() {
dialog.setMessage("Starting retrieval");
dialog.show();
}
protected JSONObject doInBackground(Url... params) {
try{
return jsonParser.getJSONFromUrl(params[0]);
}catch(Exception e){
return false;
}
return true;
}
protected void onPostExecute(final JSONObject success) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
}
Then to call this just do
public JSONObject register(){
return new JSONRetriever(this).execute(registerURL).get();
}

Do as the following:
1)Declare you dialog in activity class like this:
ProgressDialog dialog;
2)then declare you AsyncTask as below:
private class RegisterUser extends AsyncTask<String,Integer,String>{
String nessage="";
#Override
protected void onPreExecute(){
dialog = ProgressDialog.show(context, "Registering user",
"Please wait.....");
}
#Override
protected String doInBackground(String... params) {
// provide yourcode to register the user then return message
return message="you are registered";
}
protected void onPostExecute(String result) {
dialog.dismiss();
if (result.equlas("you are resgisted"))
// optinal if you want to do as below
// do something here showing toast or any thing of your prefreance
}
}

Would this help you?
#Override
public void onCreate(Bundle savedInstanceState) {
new MyAsyncTask(this).execute();
}
privateclass MyAsyncTask extends AsyncTask<String, Void, Boolean> {
private ProgressDialog dialog;
private Context context;
public ProgressTask(Context context) {
this.context= context;
dialog = new ProgressDialog(context);
}
protected void onPreExecute() {
this.dialog.setMessage("Progress start");
this.dialog.show();
}
protected void onPostExecute(final Boolean success) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
protected Boolean doInBackground(final String... args) {
// do you registering or whether here
// in this model you can return a boolean to the PostExecute.
}
}

Have a look at publishProgress() from the AsyncTask-Class i think its what you are looking for.
This method is used for updating the UI, when the Background-Thread done some Work. You can call it when ever you want it in the doInBackground()-Method.

Related

Progress Dialog in OnPreExexcute() not working

I am newbie to android and facing issue on Progress dialog,though myriad question and answers are here but none is working for me.Any help will be greatly appreciated.
I want to show a spinning wheel on my login page of app,all my Async Task activties are in different class and I am passing the activity reference to my HttpClientHelper class which is handling Async Task.No progress dialog appears when i click on Login button.
Most of the answers have mentioned to implement the Async Task in Activity class but i have created a utility class which is handling the POST/GET in background.
Hence in thin non activity class i dont have the reference of Context so i passed LoginActivity.this in HttpClientHelper constructor.
AM i doing something wrong here.Due to multiple use of POST/Get i can implement them in each activity.
Please find the sample files these are not actual code I am posting the steps
public class LoginActivity extends AppCompatActivity {
btnSignIn.setOnClickListener(new View.OnClickListener() {
HttpClientHelper httpClientHelper = new HttpClientHelper(LoginActivity.this);
JSONObject json = httpClientHelper.postJSON(apiURL
, params);
...... rest of the code
}
}
Here is the HttpClientHelper
public class HttpClientHelper {
private Activity activity;
private static HttpURLConnection urlConnection;
private static String result;
private static JSONObject jsonObject = null;
public HttpClientHelper(){
}
public HttpClientHelper(Activity activity){
this.activity=activity;
}
public JSONObject postJSON(String url, Map<String, String> params) {
Params param = new Params(url, params);
PostAsyncTask myTask = new PostAsyncTask();
try {
jsonObject = myTask.execute(param).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
return jsonObject;
}
private class PostAsyncTask extends AsyncTask<Params, String, JSONObject> {
JSONObject json = null;
ProgressDialog progressDialog;
public PostAsyncTask(){
}
#Override
protected void onPreExecute(){
super.onPreExecute();
progressDialog = new ProgressDialog(activity);
progressDialog.setTitle("Login");
progressDialog.setMessage("Loading..Please Wait");
progressDialog.setIndeterminate(true);
progressDialog.setCancelable(true);
progressDialog.show();
}
#Override
protected JSONObject doInBackground(Params... args) {
json = HttpClientHelper.getJSONFromURL(args[0].url, args[0].params);
return json;
}
#Override
protected void onPostExecute(JSONObject json) {
super.onPostExecute(json);
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
}
}
Progress dialogue is associated with UI and any updates in view is not recommended to put inside a AsyncTask. UI updates or changes should occur in the UI thread.
In your case, the desired result can be achieved by setting a listener to the AsyncTask. Simply add a listener class and implement the listener in the activity class.
Here's an example.
HttpResponseListener.java
public interface HttpResponseListener {
public void httpResponseReceiver(String result);
}
Now in your LoginActivity implement the interface like this:
public class LoginActivity extends AppCompatActivity implements HttpResponseListener {
// rest of your code
// initialize your progress dialogue here and execute the asynctask
progressDialogue.show();
myTask.execute();
myTask.mHttpResponseListener = this;
#Override
public void httpResponseReceiver(String result) {
if(result == null)progressDialogue.dismiss();
else {
// Use result to serve your purpose
progressDialogue.dismiss();
}
}
}
Now in your AsyncTask class, set the result in post execute.
private class PostAsyncTask extends AsyncTask<Params, String, JSONObject> {
JSONObject json = null;
ProgressDialog progressDialog;
public HttpResponseListener mHttpResponseListener;
public PostAsyncTask(){
}
#Override
protected void onPreExecute(){
super.onPreExecute();
}
#Override
protected JSONObject doInBackground(Params... args) {
json = HttpClientHelper.getJSONFromURL(args[0].url, args[0].params);
return json;
}
#Override
protected void onPostExecute(JSONObject json) {
super.onPostExecute(json);
mHttpResponseListener.httpResponseReceiver("OK");
}
#Override
protected void onCancelled() {
mHttpResponseListener.httpResponseReceiver(null);
}
}

How do i return a result from a async task [duplicate]

This question already has answers here:
How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?
(17 answers)
Closed 7 years ago.
I have been using async tasks to hit a web server and updating controls using the result. This has drawbacks, namely it makes the async methods specific to controls and stops me using the returned string again.
How do i return the resulting string from a async call onPostExecute? How do i call it? I cant seem to get my code able to do that. There should be no issues with threading as i have a dialog that freezes the UI until job is done.
My typical asyncTask code is as follows
class GetDataFromServer extends AsyncTask<String, String, String>
{
* */
// Progress Dialog
private ProgressDialog qDialog;
private Context context;
private String dialogString;
private ArrayList<String[]> newLoginResult;
// JSON parser class
String url_newGame ="http://xxxxxx.php";
public myAsyncMethos(String dialogMessage, Context con)
{
this.qDialog = new ProgressDialog(con);
this.dialogString = dialogMessage;
this.context = con;
}
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute()
{
super.onPreExecute();
qDialog = new ProgressDialog(this.context);
qDialog.setMessage(this.dialogString);
qDialog.setIndeterminate(false);
qDialog.setCancelable(false);
qDialog.show();
}
#Override
protected JSONObject doInBackground(String... args)
{
//MAKE SERVER CALL and cast to JSONOBject
return jsonNewUser;
}
public void onPostExecute(JSONObject jsonString)
{
// dismiss the dialog after getting response
qDialog.dismiss();
//I WANT TO RETURN A STRING HERE BUT KEEP GETTING SYNTAX ERRORS BEFORE RUNTIME
}
}
I would personally add a callback to your class, then once onPostExecute is run, fire off your callback to the listener on the main class.
class GetDataFromServer extends AsyncTask<String, String,JSONObject>
{
// Progress Dialog
private ProgressDialog qDialog;
private Context context;
private String dialogString;
private ArrayList<String[]> newLoginResult;
private InformComplete myCallback;
public GetDataFromServer(String dialogMessage, Context con,InformComplete callback)
{
this.qDialog = new ProgressDialog(con);
this.dialogString = dialogMessage;
this.context = con;
this.myCallback=callback;
}
#Override
protected void onPreExecute()
{
// set up your dialog
}
#Override
protected JSONObject doInBackground(String... args)
{
JSONObject jsonNewUser=new JSONObject();
return jsonNewUser;
}
public void onPostExecute(JSONObject jsonString)
{
qDialog.dismiss();
myCallback.PostData(jsonString);
}
public interface InformComplete
{
public void PostData(JSONObject result);
}
}
Then from your calling class, you'd have something like this...
private void callTheAsyncThing
{
GetDataFromServer gds=new GetDataFromServer("please wait", this, letMeKnow);
gds.execute(params);
}
private InformComplete letMeKnow=new InformComplete()
{
public void PostData(JSONObject result)
{
// we now have the data in the calling class
}
};
You can't return a value in methods from AsynTask cause it used to return a void element. So, you can instance global var foe example, and set the value to it. Like...
`
class GetDataFromServer extends AsyncTask<String, String, String>
{
* */
// Progress Dialog
private ProgressDialog qDialog;
private Context context;
private String dialogString;
private ArrayList<String[]> newLoginResult;
private String value;
// JSON parser class
String url_newGame ="http://xxxxxx.php";
public myAsyncMethos(String dialogMessage, Context con)
{
this.qDialog = new ProgressDialog(con);
this.dialogString = dialogMessage;
this.context = con;
}
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute()
{
super.onPreExecute();
qDialog = new ProgressDialog(this.context);
qDialog.setMessage(this.dialogString);
qDialog.setIndeterminate(false);
qDialog.setCancelable(false);
qDialog.show();
}
#Override
protected JSONObject doInBackground(String... args)
{
//MAKE SERVER CALL and cast to JSONOBject
return jsonNewUser;
}
public void onPostExecute(JSONObject jsonString)
{
// dismiss the dialog after getting response
qDialog.dismiss();
value = "Whatever you want";
}
public void setValue(String value){
this.value=value;
}
public String getValue(){
return this.value;
}
}`
And then use it. There is one way to return something. Btw you can't change return.
the method of onPostExecute is not Programmer call,it called by AsyncTask instance.if you want to return a result from a async task,you can send a parameter into GetDataFromServer ,and in method of onPostExecute,you give him the new value,and then,you can use Handler to send a message.
Your return JSONObject in doInBackground method while you try to get string in onPostExecute method.
public void onPostExecute(JsonObject jsonString)
{
// dismiss the dialog after getting response
qDialog.dismiss();
//I WANT TO RETURN A STRING HERE BUT KEEP GETTING SYNTAX ERRORS BEFORE RUNTIME
}

progress bar is not showing

I am trying to use progress bar while updating my database. Good thing, i can successfully update my database but my progress bar is not showing. I am using a progress bar the will show also the percentage on my update. I don't know what wrong with my code below, help me figure it out please:
public class SyncBrand extends AsyncTask<String, Void, Boolean>
{
public static final int BRAND_DIALOG_DOWNLOAD_PROGRESS = 0;
public SyncBrand(Context context, String _username, String _password, String _code,String _remarks,String _date,String _province,String _infotype,
String _competitor,ArrayList<String> _brands, ArrayList<String> _segments)
{
....
}
protected Dialog onCreateDialog(int id) {
switch (id) {
case BRAND_DIALOG_DOWNLOAD_PROGRESS:
progressDialog = new ProgressDialog(mContext);
progressDialog.setMessage("Updating Sub Brands..");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(false);
progressDialog.show();
return progressDialog;
default:
return null;
}
}
#SuppressWarnings("deprecation")
protected void onPreExecute()
{
super.onPreExecute();
((Activity) mContext).showDialog(BRAND_DIALOG_DOWNLOAD_PROGRESS);
}
protected Boolean doInBackground(String... arg0)
{
try{
....
}catch (Exception e){
Log.e("Update SubBrand", "Error:", e);
exception = e;
return false;
}
....
return true;
}
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC",progress[0]);
progressDialog.setProgress(Integer.parseInt(progress[0]));
}
#SuppressWarnings("deprecation")
protected void onPostExecute(Boolean valid)
{
((Activity) mContext).removeDialog(BRAND_DIALOG_DOWNLOAD_PROGRESS);
if(valid){
.....
}else{
Toast.makeText(mContext, "Failed to update.Please try again.", Toast.LENGTH_SHORT).show();
mContext.startActivity(new Intent(mContext, S_2nd_Main.class));
}
}
}
A couple problems I see here. You have Void for the second argument in your AsyncTask
public class SyncBrand extends AsyncTask<String, Void, Boolean>
which means that onProgressUpdate() should expect that type of data passed to it but you have protected void onProgressUpdate(String... progress) { which tells that method to take a String parameter. Also, you don't call publishProgress() from doInBackground() which is what is used to call onProgressUpdate().
Change your AsyncTask to
public class SyncBrand extends AsyncTask<String, String, Boolean>
and add publishProgress() to doInBackground() and pass the String value you want it to update the ProgressDialog to.

ProgressBar in AsyncTask - showing after onPostExecute method

I retreive data from server by means of external class which contains AsyncTask:
public class GetTask extends AsyncTask<String, Void, JSONObject> {
private Context context;
ProgressDialog dialog;
public GetTask(Context cxt) {
context = cxt;
dialog = new ProgressDialog(context);
}
protected void onPreExecute() {
dialog.setTitle("Load...");
dialog.setMessage("Data...");
dialog.show();
super.onPreExecute();
}
#Override
protected JSONObject doInBackground(String... url) {
// code for retreive data
return jArray;
}
protected void onPostExecute(JSONObject object) {
dialog.dismiss();
super.onPostExecute(object);
}
}
I call this task from my activity:
Tasks task = new Tasks();
JSONObject json = task.new GetTask(this).execute(ServerURL).get();
My data retrieved successfully but ProgressDialog show after super.onPostExecute(object); method, why?
P.S. dialog show after:
// Make sure the identity of this thread is that of the local process,
// and keep track of what that identity token actually is.
Binder.clearCallingIdentity();
final long ident = Binder.clearCallingIdentity();
on internal Looper.class
Sorry my english is bad. ))
I found the solution, need use callback and not use .get() method. I call my task:
callTask(linkODeatails, obj);
callTask:
void callTask(String link, String object){
task.new GetTask(this).execute(link + object);
}
I create interface:
public interface AsyncTaskCompleteListener {
public void onTaskComplete(JSONObject result);
}
And added on my task:
private Activity activity;
private AsyncTaskCompleteListener callback;
public GetTask(Activity act){
this.activity = act;
this.callback = (AsyncTaskCompleteListener)act;
}
them called:
callback.onTaskComplete(result, object);

ProgressDialog in AsyncTask

I'm trying to display a custom progressdialog while loading RSS feed from an HTTP server, I made a hard search, but nothing helped me to do this, the only thing I know is that the solution should use AsyncTask, but I'm confusing about the params to pass to this AsyncTask.
Here's my activity :
public class Soirees extends ListActivity {
private List<Message> messages;
private TextView tvSorties;
private MyProgressDialog dialog;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.sorties);
tvSorties=(TextView)findViewById(R.id.TVTitle);
tvSorties.setText("Programme des soirées");
loadFeed();
}
private void loadFeed(){
try{
BaseFeedParser parser = new BaseFeedParser();
messages = parser.parse();
List<Message> titles = new ArrayList<Message>(messages.size());
for (Message msg : messages){
titles.add(msg);
}
MessageListAdapter adapter = new MessageListAdapter(this,titles);
this.setListAdapter(adapter);
adapter.notifyDataSetChanged();
} catch (Throwable t){
Log.e("ImageLoader",t.getMessage(),t);
}
}
}
Can you please help me add AsyncTask to this?
/**
* this class performs all the work, shows dialog before the work and dismiss it after
*/
public class ProgressTask extends AsyncTask<String, Void, Boolean> {
public ProgressTask(ListActivity activity) {
this.activity = activity;
dialog = new ProgressDialog(activity);
}
/** progress dialog to show user that the backup is processing. */
private ProgressDialog dialog;
/** application context. */
private ListActivity activity;
protected void onPreExecute() {
this.dialog.setMessage("Progress start");
this.dialog.show();
}
#Override
protected void onPostExecute(final Boolean success) {
if (dialog.isShowing()) {
dialog.dismiss();
}
MessageListAdapter adapter = new MessageListAdapter(activity, titles);
setListAdapter(adapter);
adapter.notifyDataSetChanged();
if (success) {
Toast.makeText(context, "OK", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Error", Toast.LENGTH_LONG).show();
}
}
protected Boolean doInBackground(final String... args) {
try{
BaseFeedParser parser = new BaseFeedParser();
messages = parser.parse();
List<Message> titles = new ArrayList<Message>(messages.size());
for (Message msg : messages){
titles.add(msg);
}
activity.setMessages(titles);
return true;
} catch (Exception e)
Log.e("tag", "error", e);
return false;
}
}
}
public class Soirees extends ListActivity {
private List<Message> messages;
private TextView tvSorties;
private MyProgressDialog dialog;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.sorties);
tvSorties=(TextView)findViewById(R.id.TVTitle);
tvSorties.setText("Programme des soirées");
// just call here the task
AsyncTask task = new ProgressTask(this).execute();
}
public void setMessages(List<Message> msgs) {
messages = msgs;
}
}
Fixed by moving the view modifiers to onPostExecute so the fixed code is :
public class Soirees extends ListActivity {
private List<Message> messages;
private TextView tvSorties;
//private MyProgressDialog dialog;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.sorties);
tvSorties=(TextView)findViewById(R.id.TVTitle);
tvSorties.setText("Programme des soirées");
new ProgressTask(Soirees.this).execute();
}
private class ProgressTask extends AsyncTask<String, Void, Boolean> {
private ProgressDialog dialog;
List<Message> titles;
private ListActivity activity;
//private List<Message> messages;
public ProgressTask(ListActivity activity) {
this.activity = activity;
context = activity;
dialog = new ProgressDialog(context);
}
/** progress dialog to show user that the backup is processing. */
/** application context. */
private Context context;
protected void onPreExecute() {
this.dialog.setMessage("Progress start");
this.dialog.show();
}
#Override
protected void onPostExecute(final Boolean success) {
List<Message> titles = new ArrayList<Message>(messages.size());
for (Message msg : messages){
titles.add(msg);
}
MessageListAdapter adapter = new MessageListAdapter(activity, titles);
activity.setListAdapter(adapter);
adapter.notifyDataSetChanged();
if (dialog.isShowing()) {
dialog.dismiss();
}
if (success) {
Toast.makeText(context, "OK", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Error", Toast.LENGTH_LONG).show();
}
}
protected Boolean doInBackground(final String... args) {
try{
BaseFeedParser parser = new BaseFeedParser();
messages = parser.parse();
return true;
} catch (Exception e){
Log.e("tag", "error", e);
return false;
}
}
}
}
#Vladimir, thx your code was very helpful.
AsyncTask is very helpful!
class QueryBibleDetail extends AsyncTask<Integer, Integer, String>{
private Activity activity;
private ProgressDialog dialog;
private Context context;
public QueryBibleDetail(Activity activity){
this.activity = activity;
this.context = activity;
this.dialog = new ProgressDialog(activity);
this.dialog.setTitle("查询经文");
this.dialog.setMessage("正在查询:"+tome+chapterID+":"+sectionFromID+"-"+sectionToID);
if(!this.dialog.isShowing()){
this.dialog.show();
}
}
#Override
protected String doInBackground(Integer... params) {
Log.d(TAG,"经文doInBackground");
publishProgress(params[0]);
if(sectionFromID > sectionToID){
return "";
}
String queryBible = "action=query_bible&article="+chapterID+"&id="+tomeID+"&verse_start="+sectionFromID+"&verse_stop="+sectionToID+"";
try{
String bible = (Json.getRequest(HOST+queryBible)).trim();
bible = android.text.Html.fromHtml(bible).toString();
return bible;
}catch(Exception e){
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String bible){
Log.d(TAG,"经文onPostExecute");
TextView bibleBox = (TextView) findViewById(R.id.bibleBox);
bibleBox.setText(bible);
this.dialog.dismiss();
}
}
A couple of days ago I found a very nice solution of this problem. Read about it here. In two words Mike created a AsyncTaskManager that mediates ProgressDialog and AsyncTask. It's very easy to use this solution. You just need to include in your project several interfaces and several classes and in your activity write some simple code and nest your new AsyncTask from BaseTask. I also advice you to read comments because there are some useful tips.
Don't know what parameter should I use?
A lot of Developers including have hard time at the beginning writing an AsyncTask because of the ambiguity of the parameters. The big reason is we try to memorize the parameters used in the AsyncTask. The key is Don't memorize. If you can visualize what your task really needs to do then writing the AsyncTask with the correct signature would be a piece of cake.
What is an AsyncTask?
AsyncTask are background task which run in the background thread. It takes an Input, performs Progress and gives Output.
ie AsyncTask<Input,Progress,Output>
Just figure out what your Input, Progress and Output are and you will be good to go.
For example
How does doInbackground() changes with AsyncTask parameters?
How doInBackground() and onPostExecute(),onProgressUpdate() are
related?
How can You write this in a code?
DownloadTask extends AsyncTask<String,Integer,String>{
#Override
public void onPreExecute(){
}
#Override
public String doInbackGround(String... params)
{
// Download code
int downloadPerc = // calculate that
publish(downloadPerc);
return "Download Success";
}
#Override
public void onPostExecute(String result)
{
super.onPostExecute(result);
}
#Override
public void onProgressUpdate(Integer... params)
{
// show in spinner, access UI elements
}
}
How will you run this Task in Your Activity?
new DownLoadTask().execute("Paradise.mp3");
It's been a few years since this question was asked (and since someone has posted a response). Since then, ProgressDialog was deprecated in API level O, according to Android's official documentation. As such, you might consider using an inline progress bar instead of a ProgressDialog as the documentation authors suggest.
This question is already answered and most of the answers here are correct but they don't solve one major issue with config changes. Have a look at this article https://androidresearch.wordpress.com/2013/05/10/dealing-with-asynctask-and-screen-orientation/ if you would like to write a async task in a better way.

Categories

Resources