Android AsyncTask类在AlertDialog内没有响应
我试图在Android AsyncTask类在AlertDialog内没有响应,android,android-asynctask,Android,Android Asynctask,我试图在AlertDialog中运行登录类,但没有预期的结果,即使登录类没有logcat错误。希望有人能帮助我 public class MainActivity extends Activity //Inflation button1= (Button)findViewById(R.id.btnOrderSubmit); textView1= = (TextView)findViewById(R.id.editenquiry);
AlertDialog
中运行登录类,但没有预期的结果,即使登录类没有logcat错误。希望有人能帮助我
public class MainActivity extends Activity
//Inflation
button1= (Button)findViewById(R.id.btnOrderSubmit);
textView1= = (TextView)findViewById(R.id.editenquiry);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Intent method
}
});
//OnclickListener for TextView
textView1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
AlertDialog.Builder login = new AlertDialog.Builder(MainActivity.this);
login.setMessage("For Enquiry, Please Login");
login.setPositiveButton("Log In", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int arg1) {
enquirylogin();
}
});
login.setNegativeButton("Continue", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int arg1) {
//Intent here
}
});
login.show();
}
private void enquirylogin() {
// TODO Auto-generated method stub
LayoutInflater li = LayoutInflater.from(MainActivity.this);
View promptsView = li.inflate(R.layout.enqlogin, null);
AlertDialog.Builder enqLogin = new AlertDialog.Builder(MainActivity.this);
enqLogin.setView(promptsView);
enusername=(EditText) promptsView.findViewById(R.id.enqusername);
enpassword=(EditText) promptsView.findViewById(R.id.enqpassword);
enqLogin.setCancelable(true);
enqLogin.setMessage("Please Login Now");
enqLogin.setPositiveButton("Submit",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
new ELogin();
}
});
enqLogin.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
enqLogin.show();
}
Alertdialog
包含Elogin类调用的promptsView
class ELogin extends AsyncTask<String, JSONObject, JSONObject>{
ProgressDialog pDialog;
protected void onPreExecute() {
pDialog = new ProgressDialog(MainActivity.this);
}
protected JSONObject doInBackground(String... arg0) {
strELogin=enusername.getText().toString();
strEPassword=enpassword.getText().toString();
//String response =null;
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("txtUName", strELogin));
params.add(new BasicNameValuePair("txtPass", strEPassword));
JSONObject json = jsonParser.makeHttpRequest (LOGIN_URL, "POST", params);
return json;
}
protected void onPostExecute(JSONObject result) {
try {
pDialog.dismiss();
int success = result.getInt(TAG_SUCCESS);
if(success == 1) {
Log.d("Login Successful!", result.toString());
Toast.makeText(MainActivity.this, "Authentication Success",Toast.LENGTH_LONG).show();
String role_rs = result.getString(TAG_ROLE_RS);
String role_id = result.getString(TAG_CID_RS);
}else{
Toast.makeText(MainActivity.this, "try again!!!!!!", Toast.LENGTH_LONG).show();
Log.d("try Again!", result.getString(TAG_MESSAGE));
}
}catch (JSONException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
}
});
}
class-ELogin扩展了异步任务{
ProgressDialog;
受保护的void onPreExecute(){
pDialog=新建进度对话框(MainActivity.this);
}
受保护的JSONObject doInBackground(字符串…arg0){
strELogin=enusername.getText().toString();
strEPassword=enpassword.getText().toString();
//字符串响应=null;
List params=new ArrayList();
参数add(新的BasicNameValuePair(“TxtName”,strELogin));
参数add(新的BasicNameValuePair(“txtPass”,strEPassword));
JSONObject json=jsonParser.makeHttpRequest(登录URL,“POST”,参数);
返回json;
}
受保护的void onPostExecute(JSONObject结果){
试一试{
pDialog.disclose();
int success=result.getInt(TAG_success);
如果(成功==1){
Log.d(“登录成功!”,result.toString());
Toast.makeText(MainActivity.this,“身份验证成功”,Toast.LENGTH_LONG.show();
字符串角色=result.getString(标记角色);
String role\u id=result.getString(TAG\u CID\u RS);
}否则{
Toast.makeText(MainActivity.this,“重试!!!!!!!”,Toast.LENGTH\u LONG.show();
Log.d(“重试!”,result.getString(TAG_MESSAGE));
}
}捕获(JSONException e){
e、 printStackTrace();
}捕获(NullPointerException e){
e、 printStackTrace();
}
}
}
});
}
在创建ELogin
的新实例时,应该对其调用.execute()
,以启动任务
官方文档告诉您线程和异步任务的所有内容:。下面缺少:
new ELogin().execute();
您必须执行异步任务
希望这对你有帮助