Jhonatan Pereira Jhonatan Pereira -4 years ago 101
Android Question

Why Thread don't run?

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?

Answer Source

you forgot to call .start():

new Thread() { // last BreakPoint stop here
  //
}.start;
^^^^^^^
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download