Thread does not run

Asked

Viewed 52 times

0

Adding Breakpoints I could notice that the Thread below is not executed. I search the typed zip code using HTTP, returning a JSON, via the viacep.com webservice.

if (cep.length() == 8) {

    final ProgressDialog dialog = ProgressDialog.show(Inicial.this, "",
                                "Carregando CEP", true);
    dialog.show();
    new Thread() { // último BreakPoint pára aqui
        public void run() {
            try {
                //BreakPoints adicionados daqui pra baixo não executam
                String url = "https://viacep.com.br/ws/" + cep + "/json";
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                final HttpResponse resposta = httpClient.execute(httpPost);
                runOnUiThread(new Runnable() {
                    public void run() {
                        try {
                            JSONObject obj = new JSONObject(EntityUtils.toString(resposta.getEntity()));
                            EditText endereco = (EditText) findViewById(R.id.cadEndereco);
                            EditText compl = (EditText) findViewById(R.id.cadComplemento);
                            EditText bairro = (EditText) findViewById(R.id.cadBairro);
                            EditText cidade = (EditText) findViewById(R.id.cadCidade);
                            EditText uf = (EditText) findViewById(R.id.cadUF);

                            endereco.setTag(obj.getString("logradouro"));
                            compl.setText(obj.getString("complemento"));
                            bairro.setText(obj.getString("bairro"));
                            cidade.setText(obj.getString("localidade"));
                            uf.setText(obj.getString("uf"));
                        } catch (IOException | JSONException e) {
                            e.printStackTrace();
                        }
                        dialog.dismiss();
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };
}

I need to display the.show() dialog when starting and dialog.Ismiss() when finishing. Any idea why you are not running Thread?

Grateful

1 answer

2


Is missing give start() in Thread.

Do so:

Thread thread = new Thread() {
     ...
};
thread.start();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.