这是我的代码,我尝试登录我使用异步任务来完成任务。
MainActivity extends Activity {
//define controls
EditText txt_uname, txt_pwd;
TextView txt_Error;
Button btnLogin;
String response = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initalise controls
txt_uname=(EditText)findViewById(R.id.txtUsername);
txt_pwd=(EditText)findViewById(R.id.txtPassword);
btnLogin =(Button)findViewById(R.id.btnLogin);
txt_Error =(TextView)findViewById(R.id.txtError);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String uname = txt_uname.getText().toString();
String pwd = txt_pwd.getText().toString();
validateUserTask task = new validateUserTask();
task.execute(new String[]{uname, pwd});
}
}); //close on listener
}// close onCreate
private class validateUserTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("username", params[0] ));
postParameters.add(new BasicNameValuePair("password", params[1] ));
String res = null;
try {
response = CustomHttpClient.executeHttpPost("https://xyz/restapi/login", postParameters);
res=response.toString();
res= res.replaceAll("\\s+","");
}
catch (Exception e) {
txt_Error.setText(e.toString());
}
return res;
}//close doInBackground
@Override
protected void onPostExecute(String result) {
if(result.equals("1")){
//navigate to Main Menu
Intent i = new Intent(MainActivity.this, MainMenuActivity.class);
startActivity(i);
}
else{
txt_Error.setText("Sorry!! Incorrect Username or Password");
}
}
}
}
我没有得到其他API的响应,但是当我在Postman中测试时它工作正常。任何人都可以让我知道我错过了什么,让我知道是否有任何教程吗?