【发布时间】:2013-07-19 21:21:46
【问题描述】:
我的活动类中有一个 AsyncTask,当我检查 doInBackground() 中的一些数据时,我只想更改/设置我的活动类的实例变量,但不知何故,什么都没有发生变化! :( 如果变量被改变,另一个 AsyncTask 应该启动。
现在是代码:
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 文件只检查输入的电子邮件是否存在于数据库中。 那么,主要问题是如何轻松更改 doInBackground 或 onPostExecute 中的变量“emailNotAvalaible”?
感谢您的帮助!!!
编辑:
再次您好,感谢大家的帮助,更改变量工作正常,但我想我的问题是,在我的 Register AsyncTask 已经完成之前,新的 AsyncTask 证明了变量并想要启动,但就在一秒钟之后变量已设置。那么,如何确保第二个 AsyncTask 仅在第一个 AsyncTask 已完成时才启动?谢谢你们的帮助!!!
【问题讨论】:
-
你确定 doubleEmail.equals("no") 返回 true 吗?
-
尝试将 emailAvalaible.equals("no") 替换为 true 并查看变量是否发生变化。
标签: android android-activity android-asynctask environment-variables instance-variables