Fus*_*dor 10 multithreading android
我知道你不能在主线程中做网络操作,因为Android 3.0.所以,我在一个新的内部打了个电话Thread:
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
user=login.getText().toString();
password=pass.getText().toString();
params.add(new BasicNameValuePair("user", user));
params.add(new BasicNameValuePair("pass", password));
Thread thread=new Thread(){
public void run(){
try {
// Throws exception here
response=CustomHttpClient.executeHttpPost(urlogin, params);
response=response.replaceAll("\\s+","");
} catch (Exception e) {
e.printStackTrace();
}
if(response.equals("ok")){
Intent home=new Intent(c, HomeActivity.class);
home.putExtra("username", user);
startActivity(home);
Toast toast=Toast.makeText(
c, getString(R.string.welcome), Toast.LENGTH_LONG);
toast.show();
}else{
if(response.equals("fallo")){
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast toast=Toast.makeText(
c, R.string.nologin, Toast.LENGTH_LONG);
toast.show();
login.setText("");
pass.setText("");
}
});
}else if(response.equals("nologin")){
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast toast=Toast.makeText(
c, R.string.nouser, Toast.LENGTH_LONG);
toast.show();
login.setText("");
pass.setText("");
}
});
}
}
}
};
thread.run();
}
});
Run Code Online (Sandbox Code Playgroud)
但是,尽管我没有主线程(或者至少我认为......),我收到了这个例外.