永发信息网

android 如何终止异步请求

答案:1  悬赏:10  手机版
解决时间 2021-11-24 23:20
android 如何终止异步请求
最佳答案
package com.isummation.exampleapp;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.net.UnknownHostException;
 
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.json.JSONObject;
 
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
public class UserLogin extends Activity {
 
    private EditText etUsername;
    private EditText etPassword;
    private ProgressDialog progressDialog;
    private static final int PROGRESSDIALOG_ID = 0;
    private static final int SERVER_ERROR = 1;
    private static final int NETWORK_ERROR = 2;
    private static final int CANCELLED = 3;
    private static final int SUCCESS = 4;
    private String ServerResponse;
    private LoginTask loginTask;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
 
        etUsername = (EditText) findViewById(R.id.txt_username);
        etPassword = (EditText) findViewById(R.id.txt_password);
 
        Button login_button = (Button) this.findViewById(R.id.login_button);
        login_button.setOnClickListener(new OnClickListener() {
            public void onClick(View viewParam) {
                if (etUsername.getText().toString().length() == 0
                        || etPassword.getText().toString().length() == 0) {
                    Toast.makeText(getApplicationContext(),
                            "Please enter username and password",
                            Toast.LENGTH_SHORT).show();
                } else {
 
                    //Show dialog by passing id
                    showDialog(PROGRESSDIALOG_ID);
                }
            }
        });
    }
 
    protected Dialog onCreateDialog(int id) {
        switch(id) {
        case PROGRESSDIALOG_ID:
            removeDialog(PROGRESSDIALOG_ID);
 
            //Please note that forth parameter is true for cancelable Dialog
            //Also register cancel event listener
            //if the litener is registered then forth parameter has no effect
            progressDialog = ProgressDialog.show(UserLogin.this, "Authenticating",
                    "Please wait...", true, true, new OnCancelListener(){
 
                        public void onCancel(DialogInterface dialog) {
                            //Check the status, status can be RUNNING, FINISHED and PENDING
                            //It can be only cancelled if it is not in FINISHED state
                            if (loginTask != null && loginTask.getStatus() != AsyncTask.Status.FINISHED)
                                loginTask.cancel(true);
                        }
                    });
            break;
        default:
            progressDialog = null;
        }
        return progressDialog;
    }
 
    @Override
    protected void onPrepareDialog(int id, Dialog dialog) {
        switch (id) {
        case PROGRESSDIALOG_ID:
            //check if any previous task is running, if so then cancel it
            //it can be cancelled if it is not in FINISHED state
            if (loginTask != null && loginTask.getStatus() != AsyncTask.Status.FINISHED)
                loginTask.cancel(true);
            loginTask = new LoginTask(); //every time create new object, as AsynTask will only be executed one time.
            loginTask.execute();
        }
    }
 
    class LoginTask extends AsyncTask {
        @Override
        protected Void doInBackground(Void... unused) {
            try {
                ServerResponse = null; //don't forget to make it null, as task can be called again
                HttpClient httpClient = new DefaultHttpClient();
                HttpContext localContext = new BasicHttpContext();
                HttpGet httpGet = new HttpGet(
                        getString(R.string.WebServiceURL)
                                + "/cfc/iphonewebservice.cfc?returnformat=json&method=validateUserLogin&username="
                                + URLEncoder.encode(etUsername.getText()
                                        .toString(), "UTF-8")
                                + "&password="
                                + URLEncoder.encode(etPassword.getText()
                                        .toString(), "UTF-8"));
                httpClient.getParams().setParameter(
                        CoreProtocolPNames.USER_AGENT,"Some user agent string");
 
                //call it just before you make server call
                //calling after this statement and canceling task will no meaning if you do some update database kind of operation
                //so be wise to choose correct place to put this condition
                //you can also put this condition in for loop, if you are doing iterative task
 
                //now this very important
                //if you do not put this condition and not maintaining execution, then there is no meaning of calling .cancel() method
                //you should only check this condition in doInBackground() method, otherwise there is no logical meaning
                if (isCancelled())
                {
                    publishProgress(CANCELLED); //Notify your activity that you had canceled the task
                    return (null); // don't forget to terminate this method
                }
                HttpResponse response = httpClient.execute(httpGet,
                        localContext);
 
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(
                                response.getEntity().getContent(), "UTF-8"));
                ServerResponse = reader.readLine();
                publishProgress(SUCCESS); //if everything is Okay then publish this message, you may also use onPostExecute() method
            } catch (UnknownHostException e) {
                removeDialog(PROGRESSDIALOG_ID);
                e.printStackTrace();
                publishProgress(NETWORK_ERROR);
            } catch (Exception e) {
                removeDialog(PROGRESSDIALOG_ID);
                e.printStackTrace();
                publishProgress(SERVER_ERROR);
            }
            return (null);
        }
 
        @Override
        protected void onProgressUpdate(Integer... errorCode) {
            switch (errorCode[0]) {
            case CANCELLED:
                removeDialog(PROGRESSDIALOG_ID);
                Toast.makeText(getApplicationContext(), "Cancelled by user",
                        Toast.LENGTH_LONG).show();
                break;
            case NETWORK_ERROR:
                removeDialog(PROGRESSDIALOG_ID);
                Toast.makeText(getApplicationContext(), "Network connection error",
                        Toast.LENGTH_LONG).show();
                break;
            case SERVER_ERROR:
                removeDialog(PROGRESSDIALOG_ID);
                Toast.makeText(getApplicationContext(), "Server error",
                        Toast.LENGTH_LONG).show();
                break;
            case SUCCESS:
                removeDialog(PROGRESSDIALOG_ID);
                try {
                    if (ServerResponse != null) {
                        JSONObject JResponse = new JSONObject(ServerResponse);
                        String sMessage = JResponse.getString("MESSAGE");
                        int success = JResponse.getInt("SUCCESS");
                        if (success == 1) {
                            //proceed further
 
                            //you may start new activity from here
                            //after that you may want to finish this activity
                            UserLogin.this.finish();
 
                            //Remember when you finish an activity, it doesn't mean that you also finish thread or AsynTask started within that activity
                            //So you must implement onDestroy() method and terminate those threads.
                        } else {
                            //just showing invalid username password from server response
                            Toast.makeText(getApplicationContext(), sMessage,
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                } catch (Exception e){
                    Toast.makeText(getApplicationContext(), "Server error",
                            Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }
                break;
            }
        }
 
        @Override
        protected void onPostExecute(Void unused) {
 
        }
    }
 
    @Override
    protected void onDestroy(){
        //you may call the cancel() method but if it is not handled in doInBackground() method
        if (loginTask != null && loginTask.getStatus() != AsyncTask.Status.FINISHED)
            loginTask.cancel(true);
        super.onDestroy();
    }
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
寻仙要怎么同时提高会心几率与会心上限?
阳光少儿美术(沙龙街)地址在哪,我要去那里办
48x54x42mm孕囊是男生还是女生41天
农业机械化那种陶瓷碗现在值多少钱
轧怎么读音
半轴上的卡箍可以使用螺丝拧的吗
你们的单位准请长假吗?比如两个月
柿子没处理好很色怎么办
It was not until the 1960s and 1970s
求感情描写细腻的YY小说
对于异性,在乎对方才会表现拘谨 不能完全放
韭菜怕霜冻吗?
我家狗上次空窝了4个多月又发情了正常吗
自省自悟什么意思
江西上高县到湖南武冈市距离多少
推荐资讯
用完st媚眼明眸果纤眼贴膜可以不清洗吗
服务态度用什么来形容词
有人知道怎么去找心理咨询的方法吗?
当测试服务器位于远端时框架集不能预览,这个
13款锐志导航系统操作指南
有一个周长是86分米的长方形,长e2分米,这个
猜能组什么词
大众cc买1.8好还是2.0好,动力区别大吗
左一右四,上二下三是什么数字
开贡茶店一年赚多少钱
威海怎样加盟青山干果坊呢?
当看见礼堂里人真多,脑海中便想起哪些成语
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?