• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps NetworkOnMainThreadException crashes android app

madson_gr

Lurker
Feb 28, 2015
1
0
Hi, I built a webservice and tested it with SoapUI and everything is ok. Now when I try to submit the form to register an user, I get NetworkOnMainThreadException error. It looks the main thread is having to much work.

Can you help me with AsyncTask and doInBackground() method? Where and how should I do that? On my user register layout class (RegUser.java) or where the insert method is (UserDAO.java)?

RegUser.java

Java:
public class RegUser extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user);

        final EditText username = (EditText) findViewById(R.id.username);
        final EditText email = (EditText) findViewById(R.id.email);
        final EditText password = (EditText) findViewById(R.id.password);
        final EditText salt = (EditText) findViewById(R.id.salt);
        final EditText nome = (EditText) findViewById(R.id.nome);
        final EditText sobrenome = (EditText) findViewById(R.id.sobrenome);
        final EditText telefone = (EditText) findViewById(R.id.telefone);
        final EditText cel_wts = (EditText) findViewById(R.id.cel_wts);
        final EditText rua = (EditText) findViewById(R.id.rua);
        final EditText numero = (EditText) findViewById(R.id.numero);
        final EditText bairro = (EditText) findViewById(R.id.bairro);
        final EditText cidade = (EditText) findViewById(R.id.cidade);
        final EditText estado = (EditText) findViewById(R.id.estado);
        final EditText pais = (EditText) findViewById(R.id.pais);
        final EditText cpf_cnpj = (EditText) findViewById(R.id.cpf_cnpj);
        final EditText empresa= (EditText) findViewById(R.id.empresa);
        final EditText cargo = (EditText) findViewById(R.id.cargo);

        final Button cadProfile = (Button) findViewById(R.id.cadProfile);

        cadProfile.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                UserDAO dao = new UserDAO();
                boolean resultado = dao.insertUser(new User(0, username.getText().toString(),
                        email.getText().toString(),
                        password.getText().toString(),
                        salt.getText().toString(),
                        nome.getText().toString(),
                        sobrenome.getText().toString(),
                        telefone.getText().toString(),
                        cel_wts.getText().toString(),
                        rua.getText().toString(),
                        numero.getText().toString(),
                        bairro.getText().toString(),
                        cidade.getText().toString(),
                        estado.getText().toString(),
                        pais.getText().toString(),
                        cpf_cnpj.getText().toString(),
                        empresa.getText().toString(),
                        cargo.getText().toString()));

                if(resultado){
                    finish();
                }else{
                    Toast.makeText(RegUser.this, "Erro ao cadastrar :(", Toast.LENGTH_LONG).show();
                }

            }
        });

    }
}

UserDAO.java

Java:
public class UserDAO {

private static final String URL = "...meuIP:8080/Meuprojeto/services/EventoDAO?wsdl";
private static final String NAMESPACE = "...meuprojetoWS.habitodigital.com";

private static final String INSERIR = "insertUser";
private static final String EXCLUIR = "deletetUser";
private static final String ATUALIZAR = "updateUser";
private static final String BUSCAR_TODOS = "searchAllUsers";
private static final String BUSCAR_POR_ID = "searchUserById";


public boolean insertUser(User user){

SoapObject insertUser = new SoapObject(NAMESPACE, INSERIR);
SoapObject usr = new SoapObject(NAMESPACE, "user");

usr.addProperty("id", user.getId());
usr.addProperty("username", user.getUsername());
usr.addProperty("email", user.getEmail());
usr.addProperty("password", user.getPassword());
usr.addProperty("salt", user.getSalt());

usr.addProperty("nome", user.getNome());
usr.addProperty("sobrenome", user.getSobrenome());
usr.addProperty("telefone", user.getTelefone());
usr.addProperty("cel_wts", user.getCel_wts());
usr.addProperty("rua", user.getRua());
usr.addProperty("numero", user.getNumero());
usr.addProperty("bairro", user.getBairro());
usr.addProperty("cidade", user.getCidade());
usr.addProperty("estado", user.getEstado());
usr.addProperty("pais", user.getPais());
usr.addProperty("cpf_cnpj", user.getCpf_cnpj());
usr.addProperty("empresa", user.getEmpresa());
usr.addProperty("cargo", user.getCargo());

insertUser.addSoapObject(usr);

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(insertUser);

envelope.implicitTypes = true;

HttpTransportSE http = new HttpTransportSE(URL);
try {

http.call("urn:" + INSERIR, envelope);
SoapPrimitive resposta = (SoapPrimitive) envelope.getResponse();

return Boolean.parseBoolean(resposta.toString());

} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones