我在我的activity類中有一個AsyncTask,當我在doInBackground()中檢查一些數據時,我只想更改/設置我的活動類的實例變量,但不知何故沒有什麼更改! :( 而如果變量改變另一個的AsyncTask應該開始如何在onPostExecute中設置活動的實例變量?
現在,這裏是代碼:
public class LogIn extends Activity {
private boolean emailNotAvalaible;
private void setemailNotAvalaible(boolean emailNotAvalaible) {
this.emailNotAvalaible= emailNotAvalaible;
}
private Button loginBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_activity);
loginBtn = (Button) findViewById(R.id.login_btn);
loginBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new Register().execute("");
if (emailNotAvalaible== true) {
new Installation().execute("");
}
}// end of onClick()
});// end of setOnClickListener
}// end of onCreate();
public class Register extends AsyncTask<String,Integer,String>{
@Override
protected void onPreExecute() {
...
}//end of onPreExecute()
@Override
protected String doInBackground(String... params) {
ArrayList<NameValuePair> postParamsEmail = new ArrayList<NameValuePair>();
postParamsEmail.add(new BasicNameValuePair("email", email));
try {
String emailCheck = executeHttpPost("http://.../doubleEmail.php", postParamsEmail);
try {
JSONArray jsonarr = new JSONArray(emailCheck);
String emailAvalaible = jsonarr.getString(0);
if(emailAvalaible.equals("no")){ doubleEmail = "no"; }else{ doubleEmail = "yes"; }
} catch (JSONException e) {
e.printStackTrace();
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
return "String";
}// end of doInBackground()
@Override
protected void onPostExecute(String result){
super.onPostExecute(result);
if (doubleEmail.equals("no")){
LogIn.this.setEmailNotAvalaible(true);
}
}
}//end of AsyncTask class
private static HttpClient getHttpClient() {
if (mHttpClient == null) {
mHttpClient = new DefaultHttpClient();
final HttpParams params = mHttpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
}
return mHttpClient;
}//end of getHttpClient()
public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception {
BufferedReader in = null;
try {
HttpClient client = getHttpClient();
HttpPost request = new HttpPost(url);
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String result = sb.toString();
return result;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}//end of executeHttpPost()
}//end of activity class
有些代碼沒有顯示,但是這個代碼是不是爲解決重要 的。 PHP文件只是檢查,如果輸入的電子郵件不存在於數據庫中。 所以,主要的問題是我怎麼能輕易改變變量「emailNotAvalaible」在doInBackground或onPostExecute?
感謝您的幫助!
編輯:
大家好,感謝每個人的幫助下,改變變量工作正常,但我想我的問題是,在我註冊的AsyncTask的媒體鏈接完成後,新的AsyncTask樣張的變量,並希望開始,但是在變量被設置之後僅僅一秒鐘。那麼,如何確保第二個AsyncTask僅在第一個AsyncTask已完成時啓動?感謝您的幫助!
你確定doubleEmail.equals(「no」)是否返回true? – koso
嘗試用true替換emailAvalaible.equals(「no」)並查看變量是否更改。 –