为什么Thread不会运行?

时间:2017-03-22 22:01:10

标签: android java-threads

添加断点我可以看到下面的线程没有被执行。 我使用HTTP进行zipc搜索ZIPCode,通过viacep.com webservice返回JSON。

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();
            }
        }
    };
}

因此,加载对话框显示未定义的时间,因为dialog.dismiss()位于runOnUIThread上。有谁知道为什么这不起作用?

2 个答案:

答案 0 :(得分:4)

你忘了打电话.start():

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

答案 1 :(得分:1)

我将Thread加载到变量t然后你可以运行它的成员方法,如start(),join(),sleep,yield,interrupt等。

    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();