**如何在按钮onclicklistner **上调用Asynktask
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
uname = e1.getText().toString();
upwd = e2.getText().toString();
ubranch=s1.getSelectedItem().toString();
if(uname.equals("")||upwd.equals(""))
{
Toast.makeText(getApplication(), "u cant leave this
empty", Toast.LENGTH_LONG).show();
}
else
{
//i want to call test class here
}
}
});
**我一直在寻找这个,但无法得到完美的例子**
** here is the method that i have used for login**
因为我知道这个Asyntask会是一个工作线程所以Toast会使用这个线程..我不会工作请告诉我为什么它不能与工作线程一起工作
public boolean check() {
ArrayList<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("uname", uname));
nameValuePairs.add(new BasicNameValuePair("upwd", upwd));
nameValuePairs.add(new
BasicNameValuePair("uroll",s1.getSelectedItem().toString()));
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("phpfile");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("login pass1", "connection sucess");
} catch (Exception e) {
Log.e("login failed1", e.toString());
Toast.makeText(getBaseContext(), "Connect to the Internet",
Toast.LENGTH_SHORT).show();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.e("login pass 2", "Connection successfull");
} catch (Exception e) {
Log.e("login failed2", e.toString());
}
try {
JSONObject object = new JSONObject(result);
JSONArray array = object.getJSONArray("res");
if (array.length() > 0)
{
for (int i = 0; i < array.length(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
uname = jsonObject.getString("uname");
upwd = jsonObject.getString("upwd");
uroll=jsonObject.getString("uroll");
String id1=jsonObject.getString("id");
SharedPreferences sd =
getSharedPreferences("mypref3",MODE_PRIVATE);
SharedPreferences.Editor edt = sd.edit();
edt.putString("unamee", uname);
edt.putString("uroll", uroll);
edt.putString("id", id1);
edt.commit();
if(uroll.equals("Employee"))
{
Intent i1 = new Intent(login.this, emp_dashboard.class);
startActivity(i1);
}
if(uroll.equals("Manager"))
{
Intent i1 = new
Intent(login.this,managerdashboard.class);
startActivity(i1);
}
}
Toast.makeText(getBaseContext(), "login successfull ",
Toast.LENGTH_LONG).show();
e1.setText("");
e2.setText("");
}
} catch (Exception e) {
Log.e("login failed3", e.toString());
Toast.makeText(getBaseContext(), "invalid user name or
password", Toast.LENGTH_LONG).show();
}
return false;
}
** here is test class which extends Asynktask **
** i hope i have implemented it correctly **
private class test extends AsyncTask<Void, Void, Void>{
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(login.this, "",
"loading...", true, false);
}
@Override
protected Void doInBackground(Void... params) {
check(); // method that handles the response
return null;
}
@Override
protected void onPostExecute(Void result) {
progressDialog.dismiss();
}
}
答案 0 :(得分:1)
在AsyncTasks上阅读guide非常有用,因为它们非常全面。话虽如此。您可以做的最简单的事情是创建一个非常简单的自定义类来扩展AsyncTask,您只需在自定义AsyncTask的check
中调用doInBackground
即可。如果没有为您编写所有代码,您可以在AsyncTask中使用构造函数来获取用户名和密码,并在您的单击侦听器中像这样调用:
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
uname = e1.getText().toString();
upwd = e2.getText().toString();
ubranch=s1.getSelectedItem().toString();
if(uname.equals("")||upwd.equals(""))
{
Toast.makeText(getApplication(), "u cant leave this
empty", Toast.LENGTH_LONG).show();
}
else
{
new LoginAsyncTask(uname, upwd).execute();
}
}
});