如何在连接到服务器时使用KSOAP2的后台服务?

时间:2014-07-05 14:00:45

标签: android sql-server android-ksoap2

我在我的项目中工作,该项目具有SQLLIT并与SQL-server交互。在我的服务器中,我想知道:如何创建一个侦听我的sqllite表的服务并检查是否有任何表中的新记录?它使用kso​​ap2将此记录传递给服务器。

此外,我需要首先检查我的服务是否有可用的网络。

1 个答案:

答案 0 :(得分:0)

我相信你的SQLite表会从一个活动变化(不知何故需要某种用户交互)。所以我建议采用以下方法:

  1. 使用一些侦听器创建一个SQLite表,只要表执行任何CRUD操作,就会调用这些侦听器。

    public class YourSqliteTable {
    
    private TableListener listener;
    
    public void insert(Object urData){
    
    //insert data here to sqlite table
    // call the listener
      listener.onInsert(urData);
    
    }
    }
    
    public interface TableListener{
    
    public void onInsert(Object tableRow);
    
    }
    
  2. 2.创建一个处理soap消息并侦听SQLite表的服务:

    public class SoapService extend Service implements TableListener{
    
         private HanlderThread soapThread;
         private Handler       soapHandler;
    
         public void onCreate(){
    
            soapThread = new HandlerThread("handler_thread");
            soapThread.start();
            soapHandler = new Handler(soapThread.getLooper){
    
            public void handleMessage(Message msg) {
    
                switch(msg.what){
                    case(1)://send data to server
                    HttpTransportSE transporter    = new HttpTransportSE(Server.TIGA_SERVER_IP
                                                             + SOAP_URL);
                    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                    SoapSerializationEnvelope sEnv = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    
                    try { 
                        request.addProperty("data", msg.obj);
                        sEnv.dotNet = true;
                        sEnv.setOutputSoapObject(request);
                        transporter.call(NAMESPACE + METHOD_NAME, sEnv);
                        SoapObject response = (SoapObject) sEnv.getResponse();
                        return response.toString();
                    } catch (Exception e) { 
                      e.printStackTrace();
                    } finally{
                      transporter.reset();
                    }
                    break;
                }
            }
    
         }
    
         @Override
         public void onInsert(Object tableRow){
    
            Message msg = new Message();
            msg.what = 1;
            msg.obj  = tableRow;
            soapHandler.sendMessage(msg);
         }
    
    }
    
    1. 将用户活动绑定到SoapService,并且在建立绑定时(调用onServiceConnected)将服务注册为SQLite表的侦听器。

    2. 检查互联网连接是否可用:

      public boolean isNetworkOn(){

      ConnectivityManager conMan = (ConnectivityManager) 
                                   getSystemService(CONNECTIVITY_SERVICE);
      NetworkInfo activeNet = conMan.getActiveNetworkInfo();
      
      if(activeNet == null)
          return false;
      
      if(activeNet.isAvailable() && activeNet.isConnected())
          return true;
      
      return false;
      }