Java AsyncTask显示响应表单WCF
我编写了一个Restful WCF服务,它部署在IIS上。我一直试图使用Java AsyncTask显示响应表单WCF,java,c#,android,wcf,android-asynctask,Java,C#,Android,Wcf,Android Asynctask,我编写了一个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) {
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());
}
}
运行的公共类服务扩展了异步任务{
私人例外;
字符串行=”;
公共无效设置行(字符串行)
{
this.line=line;
}
公共字符串getLine()
{
回流线;
}
@凌驾
受保护的字符串doInBackground(字符串…url){
尝试
{
DefaultHttpClient httpClient=新的DefaultHttpClient();
URI=新的URI(url[0]);
HttpGet HttpGet=新的HttpGet(uri);
setHeader(“接受”、“应用程序/json”);
setHeader(“内容类型”,“应用程序/json;字符集=utf-8”);
HttpResponse response=httpClient.execute(httpget);
HttpEntity responseEntity=response.getEntity();
BufferedReader rd=新的BufferedReader(新的InputStreamReader(response.getEntity().getContent());
//而((line=rd.readLine())!=null){
//Log.d(“*****状态行***”,“Webservice:+行);
// }
而((line=rd.readLine())!=null)
{
设定线;
Log.d(“*****状态行***”,“Webservices:+getLine());
}
}
捕获(例外e)
{
e、 printStackTrace();
}
返回“字符串:”+行;
}
受保护的void onPostExecute(字符串结果){
TextView txt=(TextView)findViewById(R.id.textView1);
setText(getLine());
}
}
在代码中,我将响应写入字符串,并尝试在执行后显示它。对于一些回音,当我运行程序时,我没有得到响应,我得到一个空白的文本视图,但是消息显示在Eclipse
LogCat
中。我找不到问题,原因是什么?AsyncTask与启动它的活动相关。如果您旋转设备或应用程序进入后台,或从dock中添加或删除,则原始活动将被销毁。AsyncTask将继续执行,并响应不再可见的原始活动
您最好使用IntentService或服务调用web服务,这是一种更可靠的模式。当应用程序收到响应时,响应被写入LOGCAT并在之后无效。因此,我删除了对LOGCAT的写入,它现在可以工作了。无论如何谢谢你的帮助