I need some help on the basic architecture of my code around the login process. I'm implementing Async Http processing to get things working for ICS.
the goal of this code is for logging into my app.
- Login form submit from UI (Login.java)
- connect to our server and pass username / password via http get XML result
- parse XML results to array. (ParseXML.java)
- display result feedback in UI.
Now this all worked in my program however after trying to test with ICS which enforces Asyc HTTP connections i quickly realised my problem and led me to doubt my entire design...
the basic way it currently works:
Login.java:
class Login {
...
ParseXML myXMLParser = new ParseXML();
myXMLParser.doLogin(username, password, Login.this);
public doFinished(result) {
// update UI
}
...
}
ParseXML.java:
class ParseXML {
// class variable to hold login object for async to access
public Login loginObj;
...
public void doLogin(String _username, String _password, Login _l) {
loginObj = (Login) _l;
...
// create loginUrl string to run through async
...
new DownloadFilesTask().execute(loginUrl);
}
class DownloadFilesTask extends AsyncTask<a, b, c> {
doInBackground() {
// do stuff
// download result of http call
// parse XML
}
onPostExecute(result) {
// call the public class variable of login object i have and tell it to update the UI
// pass back the result array.
loginObj.doFinished(result);
}
}
}
I'm mostly concerned that its bad design to be doing things this way and I should simply move the XML and http connection code within my Login.java file so its all included (UI, HTTP, XML Parsing, Asyc).
Particularly i'm concerned with calling back to Login.doFinished() from onPostExecute(). is this bad for memory? I'm worried that would cause the ParseXML object to avoid garbage collection because it is now going back to Login Activity which will continue running once the user is logged in in turn holding ParseXML open.
I come from a PHP background so I have been trying to keep all my XML parsing and HTTP processing within the ParseXML
"module" so that i know where to look for changes to this.
At the moment ParseXML
handles all http work, ie, getUsers
, getChannels
, addUser
, delUser
, doLogin
etc. But Should I try moving all the code to handle XML and HTTP connections (async) within the relevant screen/ activity so they are self contained?
I really appreciate any help on this