views:

223

answers:

0

Hi,

I am using AsyncTask in my application and sometimes when I run the application I get the error as java.util.cancellationexception.

Can someone let me know the reason of this error or the way this can be removed?

import java.util.concurrent.ExecutionException;
import com.babbleville.io.BabbleVilleSyncTask;
import com.babbleville.io.GetCurrentLocation;
import com.babbleville.utils.BabbleVilleWebServiceUrl;
import com.babbleville.utils.ConstantCodes;
import com.babbleville.utils.LoadContent;
import com.babbleville.io.LoginParse;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class BabbleVilleMainActivity extends Activity implements OnClickListener
{
    private Button btnLogin = null;
    private Button btnNewUserSignUp = null;
    private Button btnForgotPwd = null;

    //private boolean loginFlag = false;
    private EditText emailId = null;  
    private EditText password = null;
    private String userName = null;   
    private String pwd = null;  
    String url = null;

    /** Called when the activity is first created. */       
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.login);

        btnLogin = (Button) findViewById (R.id.btnLogin);
        btnNewUserSignUp = (Button) findViewById(R.id.btnNewUser);
        btnForgotPwd = (Button) findViewById(R.id.btnForgotPwd);

        emailId = (EditText) findViewById(R.id.emailId);   
        password = (EditText) findViewById(R.id.password);

        //Temporary. Should be removed later on.
    emailId.setText("[email protected]");
    password.setText("sunil123");

        btnLogin.setOnClickListener(this);
        btnNewUserSignUp.setOnClickListener(this);   
        btnForgotPwd.setOnClickListener(this);
    }

/**
 *  This is the overriden method for getting the click events.
 *  @param View v = is the view on which click event this method is called. 
 *  For eg, on Login click button v will btnLogin.
 */

public void onClick(View v) 
{
    if(v == btnLogin)
    {
        userName = emailId.getText().toString().trim();
        pwd = password.getText().toString().trim();

        System.out.println("User name ==> " + userName);
        System.out.println("Password ==> " + pwd);

        if(userName ==  null || pwd == null || userName.equals("") || pwd.equals(""))
        {
            Toast toast = Toast.makeText(BabbleVilleMainActivity.this, "Please enter your Username and Password!", 50);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        }
        else
        {
            url = null;

            /*url = ConstantCodes.LOGIN_URL + ConstantCodes.ACTION + "=" + ConstantCodes.LOGIN_ACTION + "&" +
                        ConstantCodes.EMAILID + "=" + this.userName + "&" + ConstantCodes.PWD + "="  + this.pwd ;*/

            url = BabbleVilleWebServiceUrl.getLoginURL() + ConstantCodes.EMAILID + "=" + this.userName + "&" + ConstantCodes.PWD + "="  + this.pwd ; 

            System.out.println("Login URL ==> " + url);

            BabbleVilleSyncTask loginTask = new BabbleVilleSyncTask(v.getContext());

            loginTask.execute(url);

            //System.out.println("status===>"+loginTask.getStatus());

            try 
            {
                String result = loginTask.get().trim();

                System.out.println("Result returned from Login Task ==> " + result);

                LoginParse.loginParser(result);

                if(LoginParse.getLoginAck())
                {
                    /*ConstantCodes.BABBLE_START_NUM = 0;
                    ConstantCodes.BABBLE_END_NUM = 5;*/

                //  loadBabbles();

                    GetCurrentLocation location = new GetCurrentLocation(v.getContext());

                    String lat = location.getCurrentLatitude();
                    String lon = location.getCurrentLongitude();

                    System.out.println("Latitude ==> " + lat + " Longitude ==> " + lon);
                    //Babble babble_add_data = new Babble(BabbleVilleMainActivity.this,"babble");

                    LoadContent loadContent = new LoadContent(BabbleVilleMainActivity.this);
                    loadContent.loadBabbles(lat, lon);
                    loadContent.loadVilles(); 
                    //loadContent.loadUserDetails();
                    //mContext = v.getContext();

                    Intent homeScreenIntent = new Intent(BabbleVilleMainActivity.this, HomeScreenActivity.class);
                    BabbleVilleMainActivity.this.startActivity(homeScreenIntent);
                }
                else
                {
                    Toast toast = Toast.makeText(BabbleVilleMainActivity.this, "Username or Password is incorrect", 50);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                }
            }
            catch (InterruptedException e) 
            {
                e.printStackTrace();
            }
            catch (ExecutionException e) 
            {
                e.printStackTrace();
            } 
        }
    }
    else if(v == btnForgotPwd)
    {
        Intent forgetPwdIntent = new Intent(BabbleVilleMainActivity.this, ForgotPasswordActivity.class);
        BabbleVilleMainActivity.this.startActivity(forgetPwdIntent);
    }
    else if(v == btnNewUserSignUp)
    {
        Intent newUserIntent = new Intent(BabbleVilleMainActivity.this, NewUserSignUpActivity.class);
        BabbleVilleMainActivity.this.startActivity(newUserIntent);  
    }
    }
}

The exception occurs at String result = loginTask.get().trim().

Can you let me know now what is the problem?

package com.babbleville.io;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

public class BabbleVilleSyncTask extends AsyncTask<String, Void, String> 
{
private Context context = null;
private final HttpClient httpClient = new DefaultHttpClient();
private String content = null;
private String error = null;
private ProgressDialog progressDialog = null; 
private String finalResult = null;

public BabbleVilleSyncTask(Context context)
{
    this.context = context; 
    progressDialog = new ProgressDialog(this.context);
}

protected void onPreExecute() 
{
    progressDialog.setMessage("Please Wait....");
    progressDialog.show();
}

protected String doInBackground(String... urls) 
{
    try 
    {
        HttpGet httpget = new HttpGet(urls[0]);
        ResponseHandler<String> responseHandler = new   BasicResponseHandler();
        content = httpClient.execute(httpget, responseHandler);
    }
    catch (ClientProtocolException e) 
    {
        error = e.getMessage();
        Log.e("ClientProtocolException", error);
        cancel(true);
    }
    catch (IOException e) 
    {
        error = e.getMessage();
        Log.e("IOException", error);
        cancel(true);
    }

    httpClient.getConnectionManager().shutdown();

    return content;
}

protected void onPostExecute(String result) 
{
    progressDialog.dismiss();
}
}

Please let me know how can I use the result returned in onPostExecute.

Thanks & Regards

Sunil