同步适配器与内容提供商数据循环和删除

时间:2014-09-03 04:10:33

标签: android android-contentprovider android-syncadapter

我尝试使用 ContentProvider SyncAdapter ,我设置了所有文件,并且所有文件都正常运行。我实际上在做的是,我使用syncadapter将数据发送到我的服务器,但我希望它在发送后删除它,但相反,它会一直重复发送数据,直到它得到这个错误和崩溃:

09-02 21:34:57.207: E/dalvikvm-heap(8505): Generating hprof for process: com.deliveryscience.track:sync PID: 8505
09-02 21:35:12.113: E/dalvikvm(8505): can't open /data/misc/app_oom.hprof: Permission denied
09-02 21:35:12.183: E/dalvikvm-heap(8505):  hprofDumpHeap failed with result: -1 
09-02 21:35:12.183: E/dalvikvm-heap(8505): After hprofDumpHeap for process
09-02 21:35:12.183: E/dalvikvm(8505): Out of memory: Heap Size=196608KB, Allocated=189573KB, Limit=196608KB, Proc Limit=196608KB
09-02 21:35:12.193: E/dalvikvm(8505): Extra info: Footprint=195472KB, Allowed Footprint=196608KB, Trimmed=16KB

以下是我的代码,我只想要它做的是循环并删除数据库中的每一行,因为它循环。

public void sendData(ContentProviderClient provider){

        Uri uri = Uri.parse("content://com.example.track.services/ds");
        Log.d("Uri Url:", uri.toString());
        try {
            Log.e("Oya:", "I'm here in the sync");
            final Cursor cursor = provider.query(uri, null, "", null, "");
            //cursor
            int j = cursor.getCount();

            if (cursor.getCount() == 0) {
                Account newAccount = new Account("dummyAcct", "com.example.track");
                AccountManager mgr = (AccountManager) getContext().getSystemService(Context.ACCOUNT_SERVICE);
                // If the account already exists no harm is done but
                // a warning will be logged.
                mgr.addAccountExplicitly(newAccount, null, null);
                int syncOnOff = 0;
                ContentResolver.setIsSyncable(newAccount, "com.example.track.services", syncOnOff);
            }else {

                Log.w("colum", String.valueOf(cursor.getCount()));
                if (cursor.moveToFirst()) {
                    do {
                        String link = cursor.getString(cursor.getColumnIndex("token_id"));
                        String URL_Update = "http://dev.tracking.co/locations/add.json?token=" + link;
                        Log.d("Sync Url:", URL_Update);
                        StringRequest req = new StringRequest(Request.Method.POST ,URL_Update, 
                                new Response.Listener<String>() {

                                    @Override
                                    public void onResponse(String response) {
                                        // TODO Auto-generated method stub
                                        Log.e("str Response:", response);
                                    }

                        }, new Response.ErrorListener() {

                            @Override
                            public void onErrorResponse(VolleyError error) {
                                // TODO Auto-generated method stub

                            }


                        }){
                            @Override
                            protected Map<String,String> getParams(){
                                Map<String,String> params = new HashMap<String, String>();
                                params.put("device_id",cursor.getString(cursor.getColumnIndex("device_id")));
                                params.put("latitude",cursor.getString(cursor.getColumnIndex("lat")));
                                params.put("longitude", cursor.getString(cursor.getColumnIndex("long")));
                                params.put("time", cursor.getString(cursor.getColumnIndex("time")));
                     Log.d("Parameter", params.toString());
                                return params;
                            }
                        };

                        AppController.getInstance().addToRequestQueue(req);
                        //Uri id = Uri.parse(cursor.getString(cursor.getColumnIndex(MyContract._ID)));
                        //provider.delete(id, null, null);
                        cursor.moveToNext();
                    } while (cursor.moveToLast());
                }
            }

        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

此外,如何将3个内容提供商附加到一个同步适配器。而且,在我的手机的同步部分查看我的应用程序,它只允许&#34;删除&#34;,&#34;现在同步&#34;选项已禁用,但如果按下常规同步按钮,则会同步

2 个答案:

答案 0 :(得分:0)

你的循环永远进行的原因是while(cursor.moveToLast())的使用 - 当你一遍又一遍地移动到最后一行时,这总是成功的。您应该使用isAfterLast()检查是否已完成处理所有数据。

当然,您的删除语句应该在onResponse中,因此在成功发送之前不要删除记录。

答案 1 :(得分:0)

你应该应用你的cursor循环......

final Cursor cursor = provider.query(uri, null, "", null, "");

 int j = cursor.getCount();

 if(j==0){
  //do your stuff
} else {

  cursor.moveToFirst();

 do {

  //do your stuff

  cursor.moveToNext(); //Note that this statement should be the last one for your particular iteration of the loop

} while(!cursor.isAfterLast());

}

<强>编辑: 根据android开发者文档

Sync adapters run asynchronously, so you should use them with the expectation that they transfer data regularly and efficiently, but not instantaneously. If you need to do real-time data transfer, you should do it in an AsyncTask or an IntentService.

请参阅this链接