Java 在Android中更新UI时同步
我已经编写了一个必须连接到WCF服务的应用程序。从android设备到WCF服务的连接是在Java 在Android中更新UI时同步,java,android,multithreading,wcf,response,Java,Android,Multithreading,Wcf,Response,我已经编写了一个必须连接到WCF服务的应用程序。从android设备到WCF服务的连接是在异步任务中完成的。要更新主UI,我必须同步。我当前调用线程的方式是 newservicerun()。执行(“http://10.0.0.14/serv/UserManagement.svc/ping)。这样做的问题是,当我想显示JSON WCF服务的响应时(txt.setText(svcR.execute()http://10.0.0.14/serv/UserManagement.svc/register/
异步任务中完成的。要更新主UI,我必须同步。我当前调用线程的方式是
newservicerun()。执行(“http://10.0.0.14/serv/UserManagement.svc/ping)
。这样做的问题是,当我想显示JSON WCF服务的响应时(txt.setText(svcR.execute()http://10.0.0.14/serv/UserManagement.svc/register/test/test/test/test/test);
)我获取对象的内存地址com.example.wcf。ServiceRun@41f8a470
而不是显示ServerPinged
public class ServiceRun extends AsyncTask<String, Void, String>{
private Exception exception;
@Override
protected String doInBackground(String... url) {
String msg="";
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();
Log.i( "login", "received " + getResponse(response.getEntity()) );
msg=getResponse(response.getEntity());
}
catch (Exception e)
{
e.printStackTrace();
}
return msg;
}
private String getResponse( HttpEntity entity )
{
String response = "";
try
{
int length = ( int ) entity.getContentLength();
StringBuffer sb = new StringBuffer( length );
InputStreamReader isr = new InputStreamReader( entity.getContent(), "UTF-8" );
char buff[] = new char[length];
int cnt;
while ( ( cnt = isr.read( buff, 0, length - 1 ) ) > 0 )
{
sb.append( buff, 0, cnt );
}
response = sb.toString();
isr.close();
} catch ( IOException ioe ) {
ioe.printStackTrace();
}
return response;
}
}
公共类ServiceRun扩展异步任务{
私人例外;
@凌驾
受保护的字符串doInBackground(字符串…url){
字符串msg=“”;
尝试
{
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();
Log.i(“login”,“received”+getResponse(response.getEntity());
msg=getResponse(response.getEntity());
}
捕获(例外e)
{
e、 printStackTrace();
}
返回味精;
}
私有字符串getResponse(HttpEntity)
{
字符串响应=”;
尝试
{
int length=(int)entity.getContentLength();
StringBuffer sb=新的StringBuffer(长度);
InputStreamReader isr=新的InputStreamReader(entity.getContent(),“UTF-8”);
字符buff[]=新字符[长度];
int-cnt;
而((cnt=isr.read(buff,0,长度-1))>0)
{
sb.追加(buff,0,cnt);
}
response=sb.toString();
isr.close();
}捕获(ioe异常ioe){
ioe.printStackTrace();
}
返回响应;
}
}
是什么导致了这种情况?如何修复它?这不是您希望用异步任务的结果更新UI的方式,它不会工作。您可以通过覆盖AsyncTask中的onProgressUpdate()和onPostExecute()方法来更新UI。com.example.wcf.ServiceRun.toString()返回com.example.wcf。ServiceRun@41f8a470
这是普通对象。toString()实现。。。不要重复这个问题!!!!你这是什么意思?我必须将与WCF的连接放置在后台do中,并在onPostExecute
中放置TextView的UI更新吗?正如您所说的那样。如果您想在后台进程的中途将更新传递到UI,请覆盖onProgressUpdate。如果您只需要在最后传回结果,只需重写onPostExecute。由于您的UI位于不同的类中,您可能希望将侦听器传递到AsyncTask中,以便onPostExecute可以调用它以向UI报告。或者,您可以将AsyncTask设置为UI所在类的内部类,这样您就可以直接更新onPostExecute中的UI元素。会让你知道结果的