public void getDataFromHttp(final String userID, final String userPwd){
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
String tempUrl = url;
tempUrl += "?ID=" + userID+"&PW="+userPwd;
HttpGet httpGet = new HttpGet(tempUrl);
httpResponse = new DefaultHttpClient().execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() == 200)
{
resultString = EntityUtils.toString(httpResponse.getEntity());
Log.i("wow", "result="+resultString);
Toast.makeText(LoginActivity.this,resultString,Toast.LENGTH_SHORT).show();
if(resultString.equals("true")){
myHandler.sendEmptyMessage(1);
}else {
myHandler.sendEmptyMessage(0);
}
}
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
上面线程的逻辑没有执行,我尝试将Toast放在不同的位置进行检测,发现run()内部代码没有被执行。
以下是调用它的地方
public void onClick(View v) {
// TODO Auto-generated method stub
String userID = inputUsername.getText().toString().trim();
String userPwd = inputPassword.getText().toString().trim();
if(userID.equals("")||userPwd.equals("")){
Toast.makeText(LoginActivity.this, "please type your username or password", Toast.LENGTH_SHORT).show();
}else{
getDataFromHttp(userID, userPwd);
}
}
我使用的是Android Studio。
我一直困扰着很长一段时间,请帮助我,非常感激