1

Adding breakpoints I can see that the Thread below is not executed. I do the zipped search for ZIPCode using HTTP, returning a JSON, via viacep.com webservice.

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

    final ProgressDialog dialog = ProgressDialog.show(Inicial.this, "",
                                "Loading ZipCode", true);
    dialog.show();
    new Thread() { // last BreakPoint stop here
        public void run() {
            try {
                //This code isn't running
                String url = "https://viacep.com.br/ws/" + code + "/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();
            }
        }
    };
}

So, the Loading Dialog is showing for undefined time because dialog.dismiss() is on the runOnUIThread. Anybody knows why this not working?

4

2 Answers 2

4

you forgot to call .start():

new Thread() { // last BreakPoint stop here
  //
}.start;
^^^^^^^
0
1

I'd load the Thread into a variable t and then you can run it's member methods on it like start(), join(), sleep, yield, interrupt, etc.

    Thread t = new Thread() { // last BreakPoint stop here
        public void run() {
            try {
                //This code isn't running
                String url = "https://viacep.com.br/ws/" + code + "/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();
            }
        }
    };
t.start();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.