I have a doubt regarding async task in android. I have two server calls in my activity below. I need to know where should I place the below code in my activity:
new serverConnection().execute();
Also the code of my activity that are to be placed inside
@Override
protected Void doInBackground(Void... params) {
}
And the remaining code of my activity that are to be placed inside
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
I didn't know the proper way of using async task where two server calls are to done. If there is only one server call, that will not be a problem for me. But when two server calls come simultaneously, I didn't know the proper way of doing it. and I have tried many times but still fails to do correctly. Can some one help me out regarding this matter??
EDIT:::Do I have to use two async tasks in this case??
package com.example.onlineauction;
import java.util.Calendar;
import com.example.onlineauction.MainPage.serverConnection;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
ProgressDialog dialog=null;
String keyfail;
Calendar cal=Calendar.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText username1=(EditText)findViewById(R.id.username);
final EditText password1=(EditText)findViewById(R.id.password);
Button login=(Button)findViewById(R.id.login);
Button register=(Button)findViewById(R.id.register);
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new serverConnection().execute();
CallSoap cs=new CallSoap();
try{
String username=username1.getText().toString();
String password=password1.getText().toString();
keyfail="Failed login";
//Calling the server first time and getting response from server
String response=cs.calllogin(username,password);
if(response.equalsIgnoreCase("Failed login"))
{
AlertDialog.Builder ab=new AlertDialog.Builder(MainActivity.this);
ab.setTitle("Clear the errors");
ab.setMessage("Either Username or Password is Incorrect");
ab.setCancelable(false);
ab.setPositiveButton("OK I will clear it",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
AlertDialog alertdialog=ab.create();
alertdialog.show();
}
else
{
//THis is the second call to the server and getting name from server
String name=cs.retreivename(username, password);
//String[] columns = name.split(" ");
//Log.d("Count: ",count);
//assert columns.length == 2;
//String message1=columns[0];
//String message2=columns[1];
String keysuccess,keyfail = null,keyname;
keysuccess="Success";//
//keyname=message2;
Log.d("Message: ",response);
Log.d("Name of user: ",name);
if(response.equals(keysuccess))
{
Toast.makeText(MainActivity.this,"Login Success",Toast.LENGTH_LONG).show();
Intent i1=new Intent(MainActivity.this,Category.class);
SharedPreferences sp1=getSharedPreferences("My_login", MODE_PRIVATE);
Editor editor=sp1.edit();
editor.putString("name", name);
editor.putString("username",username);
editor.putString("password",password);
editor.commit();
startActivity(i1);
}
}
}
catch(Exception ex)
{
Log.d("Exception in main activity", "Activity main");
}
}
});
register.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in=new Intent(MainActivity.this, NewRegistration.class);
startActivity(in);
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.i1:
{
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
//finishFromChild(Activity Registration.class);
System.exit(0);
finish();
}
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
//This is the code for async task.
public class serverConnection extends AsyncTask<Void, String, Void> {
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
return null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.set, menu);
return true;
}
}
Whatever network related stuff you do, should be done inside the 'doInBackground()' function of the AsyncTask
If you have two calls to the server and both are independent, then you can start 2 AsyncTasks at once.
If they are dependent, you can club the two calls inside just one AsyncTask. ie. The 2nd server call starts after 1st is completed.
Edit: