异步任务响应延迟

时间:2014-08-23 18:08:45

标签: android wcf android-asynctask response

我正在尝试使用android来使用WCF服务。我按如下方式执行我的任务。

class HttpRequestTask extends AsyncTask<Void, Void, String>
{
    String msg = "";

    public void setMsg(String msg)
    {
        this.msg = msg;
    }
    public String getMsg()
    {
        return msg;
    }

    @Override
    protected String doInBackground(Void... url) {
        String line="";
        Hashing hashing = new Hashing();

        try 
        {
                DefaultHttpClient httpClient = new DefaultHttpClient();

                EditText edit_password = (EditText) findViewById(R.id.txtPasswordRegister);
                HttpGet httpget = new HttpGet("http://10.0.0.14/server/UserManagement.svc/ping");
                httpget.setHeader("Accept", "application/json");
                httpget.setHeader("Content-type", "application/json; charset=utf-8");

                HttpResponse response = httpClient.execute(httpget);
                HttpEntity responseEntity = response.getEntity();
                BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

                while((line = rd.readLine()) != null)
                {
                    setMsg(line);

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

    protected void onPostExecute(String result) {

        TextView view = (...)findViewById(...);
        view.setText(getMsg());



    }

}

我在主new HttpReqestTask().execute()方法中调用了onCreate。如在post执行中所见,结果将写入文本视图。

当我现在尝试将文本视图中的内容写入文本编辑时,我得到一个空文本编辑。重新发送的是该任务处于RUNNING状态。

HttpReqestTask task = new HttpReqestTask();
task.execute();
TextView view = (...)findViewById(...);
TextView result = (...)findViewById(...);
result.setText(view.getText().toString());

如何在任务完成后运行部分代码?我试图使用for循环来检查状态是否已完成任务,并且只有在完成时才写入文本视图但是没有用。

0 个答案:

没有答案