更新在本地使用MS SQL在服务器中掌握SQLite数据库表

时间:2013-08-02 06:46:21

标签: android synchronization android-sqlite android-ksoap2 android-syncadapter

我想用服务器中的MS SQL数据库表更新设备中的本地SQLite数据库表(主服务器)。这里使用SOAP服务。如果在服务器中插入了任何新值,则在单击按钮时,它必须同步并更新表中的新值。我想知道如何操作,下面是我用来调用Web服务的方法。

此处MANUFACTURERID和MANUFACTURERDESC是我要更新的两列。

我使用下面的链接作为参考,并且使用了一个TaskAsync.java类。我想知道我必须在该类中编写什么来调用Web服务。作为java的新手,请帮助我如何进行同步。 Thanx提前

http://stackoverflow.com/questions/14453577/sync-in-android-sqlite-and-sql-server-crud-operation-in-two-ways



**DATABASEHELPER.JAVA**

public void syncUsers(){
        try{


            String response = new **TaskAsync()**.execute("http://tempuri.org/ISave/SyncMaster").get();
            Log.d("response",response);

            String myPath = DB_PATH + DB_NAME;
            String tablename = "ManufacturerDesc";
            myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
            myDataBase.delete(tablename, null, null);

            ContentValues values = new ContentValues();
            values.put("MANUFACTURERDESC",as_Manufacturer);
            values.put("MANUFACTURERID", asManufactureId);
            //values.put("MANUFACTURERID", value);

            long status = myDataBase.insert(tablename, " ", values);
            Log.d("database", Long.toString(status));
            myDataBase.close();
        }catch (Exception e){
            e.printStackTrace();

        }

}


**SOAPWebservice.java**

public SoapPrimitive manMaster(String manmas, String manmasid)
        {
            SoapPrimitive result = null;
            try
            {
                SoapObject request = new SoapObject("http://tempuri.org/","SyncMaster");
                request.addProperty("MANUFACTURERID", manmasid);//soap object
                request.addProperty("MANUFACTURERDESC" , manmas);

                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                envelope.setOutputSoapObject(request);
                envelope.dotNet = true;

                HttpTransportSE androidHttpTransport = new HttpTransportSE(url);
                androidHttpTransport.call("http://tempuri.org/ISave/SyncMaster",envelope);
                Log.e("WEB", androidHttpTransport.toString());
                result = (SoapPrimitive)envelope.getResponse();
                return result;
            }
            catch(Exception e){
                Log.e("WebService","Error",e);
                e.printStackTrace();

                return null;
            }
        }

1 个答案:

答案 0 :(得分:0)

将以下代码放入doInbackground()

中的异步任务中
public boolean getData() 
    {
        try
        {
            SoapObject soReturn ;
            String [] ParameterName = new String[2]; 
            String [] ParameterValue = new String[2];

            ParameterName[0] = "MANUFACTURERID";   
            ParameterValue[0] = manmasid;

            ParameterName[1] = "MANUFACTURERDESC";   
            ParameterValue[1] = manmas; 

                try
                {
                    objWSConfig.SetURL(URL, WebService);
                    objWSConfig.SetSOAPAction(SoapAction, MethodName);
                    objWSConfig.SetWebService(WebService); 
                    objWSConfig.SetMethod(MethodName);

                    ws_Method = objWSConfig.getMethod();
                    ws_Namespace = objWSConfig.getNamespace();
                    ws_URL = objWSConfig.getURL(); 
                    ws_SOAPAction = objWSConfig.getSOAPAction();

                    SoapObject request = new SoapObject(ws_Namespace,ws_Method);

                    for(ArryIndex = 0 ; ArryIndex < ParameterName.length ; ArryIndex ++)
                    {
                        PropertyInfo ParaObj = new PropertyInfo();
                        ParaObj.type = PropertyInfo.OBJECT_CLASS;
                        ParaObj.namespace = ws_Namespace;
                        ParaObj.setName(ParameterName[ArryIndex].toString());
                        ParaObj.setValue(ParameterValue[ArryIndex].toString());
                        request.addProperty(ParaObj);     
                    }

                    System.setProperty("http.keepAlive", "false"); 

                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                    envelope.setOutputSoapObject(request);
                    envelope.dotNet = true;
                    //envelope.encodingStyle = SoapSerializationEnvelope.XSD;
                    envelope.setAddAdornments(true);   

                    HttpTransportSE androidHttpTransport = new HttpTransportSE(ws_URL);
                    androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

                    androidHttpTransport.call(ws_SOAPAction, envelope); 
                    androidHttpTransport.debug = true;   

            if(soReturn != null)
            {
                boolean isResult = Boolean.parseBoolean(soReturn.getProperty
                        (WebServiceMethods.Method_Name + "Result").toString());

                if(isResult)
                {
                    String ObjRetu = (String)soReturn.getProperty("ObjData_Retu");          
                    Document doc = (Document) objCommon.XMLfromString(ObjRetu);

                    NodeList nodes = doc.getElementsByTagName("Table1");
                    int TotalNodes = nodes.getLength();

                    for (int iNode = 0; iNode < TotalNodes ; iNode++) 
                    {
                        Element e = (Element) nodes.item(iNode);

                        String MANUFACTURERID = objCommon.getValueFromXML(e, "MANUFACTURERID");
                        String MANUFACTURERDESC = objCommon.getValueFromXML(e, "MANUFACTURERDESC");

                        ContentValues values = new ContentValues();
                        values.put(MANUFACTURERDESC , MANUFACTURERID);
                        values.put(MANUFACTURERDESC, MANUFACTURERDESC);

                        objDBTable.insert(values);

                    }

                    objDBTable.close();

                    return true;
                }
            }   
            else
            {
                return false;
            }
        }catch (Exception e)  
        {
            Log.e("Error : ", "Error While Getting Data");
        }
        return true ;
    }