簡體   English   中英

調用AsyncTask Android時崩潰

[英]Crash when calling AsyncTask Android

我有一個wcf服務,根據輸入(檢查用戶登錄)返回true或false。 我正在嘗試在android應用中實現該功能,但是每當我按下按鈕時,應用就會崩潰。 這是代碼:

btnLogin.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            email = editEmail.getText().toString();
            pass = editPass.getText().toString();

            if(email.matches("") || pass.matches("")){
                txtresult.setText("Please fill in the information!");
                return;
            }


            if(!isConnected){
                txtresult.setText("You don't have internet connection!");
                return;
            }

            new MyTask().execute();

            if(status.equals("true")){
                //savePreferences();
                finish();
            }
            else{
                txtresult.setText("Wrong login information!");
            }
        }
    });

和AsyncTask:

public class MyTask extends AsyncTask<Void,Void,Void> {

    String email1 = editEmail.getText().toString();
    String pass1 = editPass.getText().toString();

    @Override
    protected Void doInBackground(Void... params) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet("http://wswob.somee.com/wobservice.svc/checkLogin/"+email1+"/"+pass1);
        HttpContext localContext = new BasicHttpContext();

        HttpResponse response;

          try {

            response = httpclient.execute(httpGet, localContext);
            HttpEntity entity = response.getEntity();
            status = EntityUtils.toString(entity);



          } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }

        return null;
    }
}

那正常,突然停止工作了……由於我有2周的經驗,我不確定如何調試。 有任何想法嗎?

這條線在這里:

if(status.equals("true")){

由於尚未分配status而崩潰,拋出NullPointerException。

根據定義,AsyncTask是異步的。 這意味着一旦您進行異步調用,代碼控件將立即返回並在另一個線程中開始異步工作。

正確的方法是使用AsyncTask回調onPostExecute() ,並在那里檢查您的狀態。

public class MyTask extends AsyncTask<Void,Void,Void> {

    String email1 = editEmail.getText().toString();
    String pass1 = editPass.getText().toString();

    @Override
    protected Void doInBackground(Void... params) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet("http://wswob.somee.com/wobservice.svc/checkLogin/"+email1+"/"+pass1);
        HttpContext localContext = new BasicHttpContext();

        HttpResponse response;

          try {

            response = httpclient.execute(httpGet, localContext);
            HttpEntity entity = response.getEntity();
            status = EntityUtils.toString(entity);



          } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }

        return null;
    }

    @Override
    protected Void onPostExecute(Void result){
        super.onPostExecute(result);
        //TODO: your code here. check your status and handle from there
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM