2013-06-05 16 views
0
I have to call a webserbice in every 10 seconds for this I am using Timer. But I am getting Exception like Can't create handler inside thread that has not called Looper.prepare(). 

my code for calling ws: 

Timer timer = new Timer(); 
    TimerTask timerTask = new TimerTask() { 
     @Override 
     public void run() { 
      callGetConnectedUserWS("Please wait..."); 
     } 
     }; 
timer.schedule(timerTask, 0, 10000); 


public void callGetConnectedUserWS(String msg, int getConnectedUsersPID) { 
    if (NetworkAvailablity.getInstance().checkNetworkStatus(FriendActivity.this)) { 
      WebServiceCommunicator.getInstance().registerForServerResponse(FriendActivity.this); 
ServerRequestParams serverRequestParams = null; 
HashMap<String, String> properties = new HashMap<String, String>(); 
    properties.put("username", _myUserName); 

    serverRequestParams = new ServerRequestParams(getParent(), properties, WebServiceDetails.METHOD_NAME_GET_CONNECTED_USERS, WebServiceDetails.URL_WS_USER, msg); 
       WebServiceCommunicator.getInstance().callWebService(FriendActivity.this, serverRequestParams, getConnectedUsersPID); 

} else { 
     Constant.ShowAlertDialog("",Constant.MSG_INTERNETERROR, getParent(), false); 
    } 

}如何調用webservice的肥皂在定時器

+0

發佈callGetConnectedUserWS(「Please wait ...」)的代碼;你在線程中更新ui嗎? – Raghunandan

+0

如果您正在更新線程中的UI,然後嘗試使用asyncTask – Shruti

+0

我從服務器獲取響應並處理處理程序中的響應後更新UI – hharry

回答

0

不能從後臺線程調用

Constant.ShowAlertDialog("",Constant.MSG_INTERNETERROR, getParent(), false);

。您需要顯示來自UI線程的警報對話框

runOnUIThread(new Runnable() { 
@Override 
void run() { 
    Constant.ShowAlertDialog("",Constant.MSG_INTERNETERROR, getParent(), false); 
    } 
}