I am new to android development. I have a AsyncTask function in my application. Calling http request from all activities. Now in each activity I am using the following class to connect to server, in some activities I even called twice !!.
Basically I am a web developer and in such cases we use a single class which can be accessed from entire application(web) and use the common function to do the same activity. The only difference is input and out put will be changed.
My doubt is in this case can I use ( convert) this to such a function or class ? My assume is
- Create an android class ( which can be accessed from all the activities )
- Just make the JSON string we need with specific server ( for process in server )
- Just pass the created json to the created class and then made the http connect )
- Process the returned data from server
- Pass that to the corresponding activity
So that I can use the same function for all the activities and I can avoid duplicate query
Can I convert this code to such a manner ?
My Code
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);
LogIN loginUser = new LogIN();
LoginUser.execute("");
}
private class LogIN extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... sUrl) {
try {
String path = "http://www.domain_name.com/app/checkSession.php";
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000);
HttpResponse response;
JSONObject json = new JSONObject();
try {
HttpPost post = new HttpPost(path);
json.put("access_token", "123456");
post.setHeader("json", json.toString());
StringEntity se = new StringEntity(json.toString());
se.setContentEncoding((Header) new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
response = client.execute(post);
/* Checking response */
if (response != null) {
InputStream in = response.getEntity().getContent();
String a = convertStreamToString(in);
JSONObject jsono = stringToJsonobj(a);
String passedStringValue = jsono.getString("result");
if(passedStringValue.equals("1")){
flags=1;
//Log.v("TAGG", "Success");
}
else {
flags=0;
//Log.v("TAGG", "Failed !");
}
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialogue("Login Processing", "Loading");
}
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
}
@Override
protected void onPostExecute(String result) {
if(flags.equals(1)){
Itent homepage = new Intent(MainActivity.this, RegisterDevice.class);
startActivity(homepage);
finish();
}
else {
Intent homepage = new Intent(MainActivity.this, LoginActivity.class);
startActivity(homepage);
finish();
}
super.onPostExecute(result);
}
}
}
Please any one help/advise Thanks in advance
On the other hand you can use this great framework for android : android-query and the async API. It allows you to perform asynchroneous network tasks from activities and easily work with the results of your requests.
You can do it like make separate class for everything inside doInBackground() method and called it in all activity with passing parameter to
and check parameter in AsyncTask Class constructor like
Extract your class to a different file and make it public
Regarding your other question, I normally have an interface for that, something like this:
I implement the interface in the class where i need the postExecute result and in the overriden method you can to what you want with the result of your task. Your class where you user it will look something like this:
You should use interfaces to implement a callback to your ui activity. Have a look at this thread, it might be useful: android asynctask sending callbacks to ui And your asyntask class should be in a seperate java file with public acces.
And to pass the parametres you simply have to call a new LogIN async Task like this:
Hope it helped :)
First of all You have to pass the context in which you are calling your async task
Sample Code
You class Login should have a constructor that accepts the Context
Sample Code
Then make sure to create the method showDialog inside Login class to show the progress dialog
Note You can add what ever Constructors you need to customize the behaviour of your Login task for example : pass boolean parameter to tell that the Login Task is cancelable....
Hope that help you :)
Remember that you can never know when AsyncTask is going to finish. So if you're using this to authenticate users and then perform task X, task Y, or task Z,
then maybe it's better to create a Login helper class
public class LoginHelper {
}
and then have in your Activity classes
private class X extends AsyncTask {
}