2011-09-27 97 views
3

我在Android中遇到了AsyncTasks的一些問題。我搜索了一個解決方案的小時,但我沒有得到它... 任務應通過HTTP檢查一個帳戶存在,並通過SharedPreferences安全訪問密鑰。Android AsyncTask似乎在onPreExecute後停止

這裏是的AsyncTask代碼:(!沒有集成類)

package de.test.barcode; 

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 

import android.content.SharedPreferences; 
import android.os.AsyncTask; 
import android.view.View; 
import android.widget.ProgressBar; 

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

    private String username, password; 
    private ProgressBar progressBar; 
    private SharedPreferences accPrefs; 

    public LoginTask(String username, String password, ProgressBar progressBar,  SharedPreferences accPrefs) { 
     this.username = username.trim(); 
     this.password = password.trim(); 
     this.progressBar = progressBar; 
     this.accPrefs = accPrefs; 
    } 

    protected Void doInBackground(Void... params) { 
     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
     nameValuePairs.add(new BasicNameValuePair("username", username)); 
     nameValuePairs.add(new BasicNameValuePair("password", password)); 
     InputStream is = null; 

     try{ 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://{URL}"); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      is = entity.getContent(); 
     }catch(Exception e){ 
      SharedPreferences.Editor prefEditor = accPrefs.edit(); 
      prefEditor.putBoolean("is_using_account", true); 
      prefEditor.putString("username", username); 
      prefEditor.putString("password", password); 
      prefEditor.putString("access_key", "null"); 
      prefEditor.commit(); 
     } 

     try{ 
      BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8")); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      line = reader.readLine(); 
      sb.append(line); 
      is.close(); 
      String accessKey = sb.toString(); 
      if(!accessKey.equals("null")) { 
       SharedPreferences.Editor prefEditor = accPrefs.edit(); 
       prefEditor.putBoolean("is_using_account", true); 
       prefEditor.putString("username", username); 
       prefEditor.putString("password", password); 
       prefEditor.putString("access_key", accessKey); 
       prefEditor.commit(); 
      } 
     }catch(Exception e){ 
      SharedPreferences.Editor prefEditor = accPrefs.edit(); 
      prefEditor.putBoolean("is_using_account", true); 
      prefEditor.putString("username", username); 
      prefEditor.putString("password", password); 
      prefEditor.putString("access_key", "null"); 
      prefEditor.commit(); 
     } 
     return null; 
    } 

    protected void onPreExecute(){ 
     progressBar.setVisibility(View.VISIBLE); 
    } 

    protected void onPostExecute() { 
     progressBar.setVisibility(View.GONE); 
    } 
} 

這裏是它(應該)執行的AsyncTask行:(所有的元素都正確的定義)

LoginTask login = new LoginTask(username, password, progressBar, accountSettings); 
login.execute(); 
+1

在每個AsyncTask方法中創建一些日誌消息,以查看它到底有多遠。 –

+0

你怎麼知道它停止了? –

+0

謝謝。問題似乎是'accessKey'是'null'。但我確定'sb'不爲空:@ – TheVaan

回答