Monday, 9 September 2013

android.os.networkonmainthreadexception inside a new Thread

android.os.networkonmainthreadexception inside a new Thread

I am aware that you can't do network operations in the main thread, since
Android 3.0. So, i made my call inside a new 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 {
response=CustomHttpClient.executeHttpPost(urlogin,
params);<---Throws exception here
response=response.replaceAll("\\s+","");
} catch (Exception e) {
// TODO Auto-generated catch block
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();
}
});
But, I receive that exception despite im NOT on main thread (or at least i
think that...)
Anybody can help? Thank you.

No comments:

Post a Comment