I have read through nearly every post on here about using publishProgress to set a new message on a progress dialog and I simply cannot get it to work.
Code:
public class LoginToApi extends AsyncTask<String, TaskProgress, String> {
protected void onPreExecute() {
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setCancelable(false);
pDialog.setMessage("Logging you in...");
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
HashMap<String, String> loginMap = new HashMap<String, String>();
loginMap.put( "username", un.getText().toString() );
loginMap.put( "password", pw.getText().toString() );
loginMap.put( "session_id", "zOhAVPADzsGk0MZiMdXUzYLErP44pVZGWdHU7yD_Paek169umn");
String loginXmlString = xmlCon.createXML("login", loginMap);
String playlistDetails = xmlParse.makeHttprequest(url_login, loginXmlString);
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS + "/adgen");
File loginFile = new File(path, "login.xml");
publishProgress(new TaskProgress(30, "A new update"));
try {
if(saveStringToFile(path, loginFile, playlistDetails)) {
Log.i("main", "login xml Saved");
// Read play.xml to array
//xmlParse.parse(file);
// Get playlist using playlist ID
HashMap<String, String> playlistMap = new HashMap<String, String>();
playlistMap.put("id", "158");
String playlistDetailsString = xmlCon.createXML("playlist", playlistMap);
String playlist = xmlParse.makeHttprequest(url_get_playlist, playlistDetailsString);
File playlistFile = new File(path, "playlist.xml");
try {
if(saveStringToFile(path, playlistFile, playlist)) {
Log.i("main", "playlist xml Saved");
}
} catch (Exception e) {
e.printStackTrace();
}
}
//Intent i = new Intent(getApplicationContext(), WebViewActivity.class);
//startActivity(i);
//finish();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void onProgressUpdate(TaskProgress progress) {
pDialog.setProgress(progress.percentage);
pDialog.setMessage(progress.message);
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
pDialog.dismiss();
}
}
private static class TaskProgress {
final int percentage;
final String message;
TaskProgress(int percentage, String message) {
this.percentage = percentage;
this.message = message;
}
}
I get zero errors or warnings, my code runs fine, with my files being saved and eventually the progress dialog being removed after the Async task is complete. but the dialog message simply wont update when I use publishProgress.
I used this post as a guide, but as you can see, it looks like the OP had trouble even after a solution was given.
What am I doing wrong?
Thanks1