-2
這是我第一次使用的AsyncTask類 我有使用的AsyncTask類來獲得一個簡單的登錄活動與外部數據庫,因此我需要連接到檢查,如果用戶名和密碼是否正確就是檢查用戶名和密碼是否正確,以便執行某些操作,並且必須在onPostExecute方法中執行此檢查,如我所想。如何使用的AsyncTask類
那麼如何做這個檢查任何人都可以幫助我?
MainActivity.java類登錄延伸的AsyncTask {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
String user = username_edt.getText().toString();
String password = pass_edt.getText().toString();
// String description = inputDesc.getText().toString();
String result = user.toString() + password.toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("user", user));
params.add(new BasicNameValuePair("password", password));
// params.add(new BasicNameValuePair("description", description));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_login_user,
"POST", params);
// check log cat from response
// Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Intent in = new Intent(getBaseContext(), NewActivity.class);
in.putExtra("ShowResult", result);
startActivity(in);
} else {
// failed
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String result) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
你現在的代碼有什麼問題?你可以在'doInBackground()'中檢查,但是如果你已經成功了,你應該把你的'success'變量傳遞給'onPostExecute()'並且在那裏啓動'Activity'。另外,你不需要在'user'和'password'上使用'toString()''因爲它們已經是'String'類型 – codeMagic
如何將變量從doInBackground傳遞給onPostExecute對於虛擬問題抱歉 – user3006788
我編輯我的問題後,我試過這是,但它沒有工作 – user3006788