請幫助如何使用這個。我正在進行登錄並驗證所有錯誤和可能的異常。這是我的代碼`在asynctask內烤麪包
EditText un, pw;
ImageButton login;
TextView error;
ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
un=(EditText)findViewById(R.id.txtUsername);
pw=(EditText)findViewById(R.id.txtPassword);
login=(ImageButton)findViewById(R.id.btnLogin);
error=(TextView)findViewById(R.id.errTxt);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new login().execute();
}
});
}
public class login extends AsyncTask{
@Override
protected Object doInBackground(Object... params) {
// TODO Auto-generated method stub
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("username", un.getText().toString()));
postParameters.add(new BasicNameValuePair("password", pw.getText().toString()));
//String valid = "1";
String response = null;
try {
response = CustomHttpClient.executeHttpPost("http://www.cjcld.org/dvts1/mobile/login.php", postParameters);
String res=response.toString();
// res = res.trim();
res= res.replaceAll("\\s+","");
//error.setText(res);
if(Integer.parseInt(res)>0){
postParameters.add(new BasicNameValuePair("id", res));
String result = null;
try
{
result = CustomHttpClient.executeHttpPost("http://www.cjcld.org/dvts1/mobile/status.php", postParameters);
String res2=result.toString();
res2= res2.replaceAll("\\s+","");
if(Integer.parseInt(res2)>0){
Intent myIntent = new Intent(getApplicationContext(), dashboard.class);
Bundle logval = new Bundle();
logval.putString("uid", res);
logval.putString("did", res2);
myIntent.putExtras(logval);
startActivity(myIntent);
}else{
Toast.makeText(getApplicationContext(),"No Delivery", Toast.LENGTH_LONG).show();
}
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_LONG).show();
}
}
else
{
Toast.makeText(getApplicationContext(),"Sorry!! Incorrect Username or Password", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_LONG).show();
}
return null;
}
}
當祝酒要執行時,程序將關閉並提示錯誤。
非常感謝。這有助於我的問題。 – 2013-02-12 00:49:51
當然!如果這有幫助,你可以通過在我的帖子的左上角打勾來標記我的帖子:-) – 2013-02-12 02:26:57
我想感謝你...你救了我的生命^^ – 2013-03-08 08:53:42