如何从webservice中仅获取新添加的数据?

时间:2016-08-05 11:16:22

标签: android .net sql-server web-services sqlite

我从.net webservice(从SQL数据库获取数据)获取记录并将它们存储到sqlite数据库以脱机显示它们。假设我从SQL数据库中获取了所有记录,但是如果我向SQL添加了一条新记录。

在第二次抓取时,为了减少抓取时间,我不想获取所有记录。我想只获取新添加的记录并将其存储到数据库。我用来获取记录的编码片段如下所述。

public class AsyncCallSoap extends AsyncTask<String,Void,String> {
    private final ProgressDialog dialog=new ProgressDialog(HomeScreen.this);

    @Override
    protected String doInBackground(String... params) {
        CallSoap com=new CallSoap();
        String SOAP_ACTION="http://tempuri.org/IService/GetContactOfflineData";
        String OPERATION_NAME="GetContactOfflineData";
        String ACTION_BUS="http://tempuri.org/IService/SelectSTTimeData";
        String OPERATION_BUS="SelectSTTimeData";
        String ACTION_TRAIN="http://tempuri.org/IService/SelectTrainTimeData";
        String OPERATION_TRAIN="SelectTrainTimeData";
        String response = com.getContacts(OPERATION_NAME,SOAP_ACTION);
        String response_bus = com.getBus(OPERATION_BUS,ACTION_BUS);
        String response_train=com.getTrain(OPERATION_TRAIN,ACTION_TRAIN);

        if(response!=null){
            try {
                myDb=new DatabaseHelper(HomeScreen.this);
                context.deleteDatabase(myDb.DATABASE_NAME);
                myDb=new DatabaseHelper(HomeScreen.this);

                JSONArray array1 = new JSONArray(response);

                for(int i=0;i<array1.length();i++)
                {
                    JSONObject obj1 = array1.getJSONObject(i);
                    String category = obj1.getString(TAG_CATEGORY);
                    String subcat = obj1.getString(TAG_SUBCAT);
                    String taluka = obj1.getString(TAG_TALUKA);
                    String name = obj1.getString(TAG_NAME);
                    String address = obj1.getString(TAG_ADDRESS);
                    String mobile = obj1.getString(TAG_MOBILE);
                    String email = obj1.getString(TAG_EMAIL);
                    String firm = obj1.getString(TAG_FIRM);
                    String desc = obj1.getString(TAG_DESC);
                    String land = obj1.getString(TAG_LAND);
                    String fax = obj1.getString(TAG_FAX);
                    String web = obj1.getString(TAG_WEB);

                    boolean isInserted = myDb.insertData(category,subcat,taluka,name,address,
                            mobile,email,firm,desc,land,fax,web);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //Showing progress dialog
        dialog.setMessage("Fetching data...");
        dialog.setCancelable(false);
        dialog.show();
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        dialog.dismiss();
    }
}

类callSoap是

public class CallSoap {
String namespace = "http://tempuri.org/";
private String url = "http://aaplasatara.absoftwaresolution.com/Service.svc";

String SOAP_ACTION;
SoapObject request = null, objMessages = null;
SoapSerializationEnvelope envelope;
AndroidHttpTransport androidHttpTransport;

CallSoap() {
}

/**
 * Set Envelope
 */
protected void SetEnvelope() {

    try {

        // Creating SOAP envelope
        envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        //You can comment that line if your web service is not .NET one.
        envelope.dotNet = true;

        envelope.setOutputSoapObject(request);
        androidHttpTransport = new AndroidHttpTransport(url);
        androidHttpTransport.debug = true;

    } catch (Exception e) {
       // System.out.println("Soap Exception---->>>" + e.toString());
    }
}

// MethodName variable is define for which webservice function  will call
public String getContacts(String MethodName,String SOAP_ACTION)
{
    try {
        //Adding values to request object
        request = new SoapObject(namespace, MethodName);

        SetEnvelope();

        try {
            //SOAP calling webservice
            androidHttpTransport.call(SOAP_ACTION, envelope);

            //Got Webservice response
            String result = envelope.getResponse().toString();

            return result;

        } catch (Exception e) {
            // TODO: handle exception
            return null;
        }
    } catch (Exception e) {
        // TODO: handle exception
        return null;
    }
}
}

0 个答案:

没有答案