Android封装WebService的简单调用并且实时更新新UI


最近做一个电力项目的客户端,要求是全部使用webservice调用他们的服务端的数据,然后展现出来。所以我就封装了一个调用webservice的类,这个还是比较的简单的类,没有多牛的地方。其中,项目中一个模块就是实时获取运营总览的一些情况,这又用到handler。下面就是相关代码,代码还有许多要完善的地方,请指出。

  1. public class HandlerService{  
  2.   
  3.     private String jsonUrl;   //webservice查询地址   
  4.     private String jsonParames; //查询参数   
  5.     private String queryMethod; //调用方法   
  6.     private String nameSpace;  //命名空间   
  7.     private String queryProperty;//查询属性   
  8.       
  9.     private String json;  
  10.     private SoapObject soapObject;  
  11.     private boolean isProperty;     //请求是否带有参数   
  12.       
  13.     public HandlerService(String jsonUrl, String jsonParames,  
  14.             String queryMethod, String nameSpace, String queryProperty,boolean isProperty) {  
  15.         super();  
  16.         this.jsonUrl = jsonUrl;  
  17.         this.jsonParames = jsonParames;  
  18.         this.queryMethod = queryMethod;  
  19.         this.nameSpace = nameSpace;  
  20.         this.queryProperty = queryProperty;  
  21.         this.isProperty = isProperty;  
  22.         getSoap();  
  23.     }  
  24.   
  25.       
  26.     public void testupdate(){  
  27.         System.out.println("testupdate");  
  28.     }  
  29.       
  30.       
  31.      private void getSoap(){  
  32.          SoapObject request = new SoapObject(nameSpace,queryMethod);   
  33.          if(this.isProperty){  
  34.              request.addProperty(queryProperty, jsonParames);  
  35.              }  
  36.            
  37.          SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);   
  38.          envelope.bodyOut = request;  
  39.          envelope.setOutputSoapObject(request);  
  40.          envelope.encodingStyle="UTF-8";  
  41.            
  42.          HttpTransportSE ht = new HttpTransportSE(jsonUrl);  
  43.          try {  
  44.             ht.call(null, envelope);  
  45.             soapObject = (SoapObject) envelope.bodyIn;  
  46.             json=soapObject.getProperty("out").toString();  
  47.             System.out.println("soapObject="+soapObject.getProperty("out").toString());  
  48.               
  49.         } catch (IOException e) {  
  50.             // TODO Auto-generated catch block   
  51.             e.printStackTrace();  
  52.         } catch (XmlPullParserException e) {  
  53.             // TODO Auto-generated catch block   
  54.             e.printStackTrace();  
  55.         }  
  56.            
  57.            
  58.      }  
  59.        
  60.        
  61.      /** 
  62.       * 获得JSON传来的数据,便于外部调用 
  63.       */  
  64.        
  65.      public String getJson(){  
  66.          return this.json;  
  67.      }  
  68. }  

下面是HTTP请求的相关类:HttpConnectResponse.java

  1. public class HttpConnectResponse {  
  2.     //httpCode=10000;这个的意义就是告诉外部,调用产生了异常   
  3.     public static int HttpIsAlive(String url){  
  4.            
  5.         int httpCode;//HTTP连接状态码   
  6.           
  7.         //创建Http Get连接   
  8.         HttpGet httpRequest = new HttpGet( url );  
  9.         try  
  10.         {  
  11.             //发出HTTP request   
  12.             HttpResponse httpResponse = new DefaultHttpClient().execute( httpRequest );  
  13.             httpCode=httpResponse.getStatusLine().getStatusCode();  
  14.   
  15.         }  
  16.         catch ( ClientProtocolException e )  
  17.         {  
  18.           
  19.         httpCode=10000;  
  20.          System.out.println("ClientProtocolException");  
  21.             //System.out.println( e.getMessage().toString() );   
  22.         e.printStackTrace();  
  23.   
  24.         }  
  25.         catch ( IOException e )  
  26.         {  
  27.          httpCode=10000;  
  28.         }  
  29.         catch ( Exception e )  
  30.         {  
  31.             httpCode=10000;  
  32.             e.printStackTrace();  
  33.         }  
  34.           
  35.         return httpCode;  
  36.   
  37.     }  
  38.       
  39. }  

最后是调用的类

  1. public class PhoneRecordActivity extends Activity implements Runnable{  
  2.     private static final String URL="xxxxxxx";  
  3.       
  4.     private String PhoneRecordString;   //从服务端返回的JSON字符串   
  5.     private HandlerService handlerService;  
  6.       
  7.     private JSONObject json;  
  8.     private int isQueryNetOk = 1;  
  9.     private Handler handler;  
  10.     int i=1;  
  11.     //private    
  12.       
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         this.setContentView(R.layout.phonerecord_view);  
  16.   
  17.           
  18.         /* 初始化View */  
  19.         initView();  
  20.         Toast.makeText(PhoneRecordActivity.this"数据加载中......", Toast.LENGTH_LONG).show();  
  21.         handler = new Handler();  
  22.         handler.postDelayed(this,1000);  
  23.           
  24.     }  
  25.   
  26.   
  27.     public void run() {  
  28.         // TODO Auto-generated method stub   
  29.         // phonerecord_id.setText(i++ + "");   
  30.         System.out.println("HttpIsAlive="  
  31.                 + HttpConnectResponse.HttpIsAlive(URL + "?wsdl"));  
  32.         int HttpIsAlive = 0;  
  33.         try{  
  34.             do {  
  35.                 HttpIsAlive = HttpConnectResponse.HttpIsAlive(URL + "?wsdl");  
  36.                 isQueryNetOk++;  
  37.                 System.out.println("isQueryNetOk="+isQueryNetOk);  
  38.                 handler.postDelayed(this1000);  
  39.   
  40.             } while (isQueryNetOk < 2 && HttpIsAlive != 200);    //就使用一次请求   
  41.               
  42.             if (HttpIsAlive == 200) {  
  43.                 showJsonData();  
  44.             }else{  
  45.                 handler.removeCallbacks(this);  
  46.                 Toast.makeText(PhoneRecordActivity.this,"网络有问题",Toast.LENGTH_LONG).show();  
  47.             }  
  48.               
  49.         }catch(Exception e){  
  50.             handler.removeCallbacks(this);  
  51.             Toast.makeText(PhoneRecordActivity.this,"网络有问题",Toast.LENGTH_LONG).show();  
  52.         }  
  53.           
  54.           
  55.     }  
  56.     private void  showJsonData(){  
  57.         handlerService = new HandlerService(URL,  
  58.                 "{/"pwd/":/"123123/",/"userName/":/"0224809111/",/"date/":/"201105/"}",  
  59.                 "queryCallOperation",  
  60.                 "http://webservice.service.system.dlsh.syit.com",  
  61.                 "jsonString",  
  62.                 false  
  63.                 );  
  64.         PhoneRecordString = handlerService.getJson();  
  65.         System.out.println("PhoneRecordString="+PhoneRecordString);  
  66.         try {  
  67.             json = new JSONObject(PhoneRecordString);  
  68.             multimediaNum = json.getString("multimediaNum");  
  69.         } catch (JSONException e) {  
  70.             // TODO Auto-generated catch block   
  71.             //e.printStackTrace();   
  72.         }catch (Exception e) {  
  73.               
  74.         }  
  75.           
  76.         XX.settext("dddddddddddd");//更新页面   
  77.           
  78.     }  
  79.       
  80.     private void initView() {  
  81.     }  
  82.       
  83.       
  84.       
  85. }  

因为还在项目中,所以不能公布完整的,只能说个大概,也请高手指点其中的不足。另外,webservice需要ksop2大家可以去自己下载。

相关内容