显示响应表单WCF

本文关键字:WCF 表单 响应 显示 | 更新日期: 2023-09-27 18:14:22

我编写了一个Restful WCF服务,并将其部署在IIS上。我一直试图使用AsyncTask线程消费WCF服务。我在主UI类中构建了线程,这样我就可以更新GUI。

public class ServiceRunning extends AsyncTask<String, Void, String>{
    private Exception exception;
    String line = "";
    public void setLine(String line)
    {
        this.line = line;
    }
    public String getLine()
    {
        return line;
    }
    @Override
    protected String doInBackground(String... url) {
        try 
        {
                DefaultHttpClient httpClient = new DefaultHttpClient();
                URI uri = new URI(url[0]); 
                HttpGet httpget = new HttpGet(uri);
                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) {
                 //   Log.d("****Status Line***", "Webservice: " + line);
             // }
                while((line = rd.readLine()) != null)
                {
                    setLine(line);
                    Log.d("****Status Line***", "Webservices: " + getLine());
                }

        }
        catch (Exception e) 
        {
              e.printStackTrace();
        }
        return "String :" + line;
    }
    protected void onPostExecute(String result) {
        TextView txt = (TextView)findViewById(R.id.textView1);
        txt.setText(getLine());
    }

}

在代码中,我将响应写入String,并尝试在执行后显示它。对于一些声音,当我运行程序时,我没有得到响应,我得到一个空白的TextView,但消息显示在Eclipse LogCat中。我找不到问题,是什么原因?

显示响应表单WCF

AsyncTask绑定到启动它的活动。如果你旋转设备,或者应用进入后台,被添加或从基座上移除,原始的活动就会被破坏。AsyncTask继续运行,并响应不再可见的原始活动。

你最好使用IntentService或Service来调用web服务,这是一个更可靠的模式。