android - Volley 库 vs Android SyncAdapter

标签 android synchronization android-volley android-syncadapter

我开发了一个使用 sqlite 数据库的应用程序。应用程序需要在以下事件后通信/同步(master-master)一些数据库表:

  • 在应用程序启动时
  • 点击刷新按钮
  • 如果用户执行某些操作会更改 sqlite 数据库中的数据

如果应用程序未运行,则无需同步 Android sqlite 数据库。每个同步请求都需要扩展 Authorization headerVolley 库已集成到应用程序中。

问题是(考虑上面的用例)使用 Volley 库在应用程序和服务器之间进行通信或实现 AsyncAdapter 哪个更好?可以结合这两种方法吗?

最佳答案

SyncAdapter 应该像这样使用 volley 来获取数据:

public class SyncAdapter extends AbstractThreadedSyncAdapter {

     @Override
    public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, final SyncResult syncResult) {
  RequestQueue queue = VolleyService.getInstance(this.getContext()).getRequestQueue();
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                // we got the response, now our job is to handle it
                try {
                    //Parse JSON response and Insert/Update data to SQLite DB
                } catch (RemoteException | OperationApplicationException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                //something happened, treat the error.
            }
        });

        queue.add(request);

    }

}

SyncAdapter 用于按需或定期同步数据。在您的内容解析器上,您可以设置刷新类型和周期:

ContentResolver.addPeriodicSync(account, "com.android.app", params, 150);
ContentResolver.setSyncAutomatically(account, "com.android.app", true);

Volley 服务:

public class VolleyService {

    private static VolleyService instance;
    private RequestQueue requestQueue;
    private ImageLoader imageLoader;

    private VolleyService(Context context) {
        requestQueue = Volley.newRequestQueue(context);

        imageLoader = new ImageLoader(requestQueue, new ImageLoader.ImageCache() {
            private final LruCache<String, Bitmap> cache = new LruCache<String, Bitmap>(20);

            @Override
            public Bitmap getBitmap(String url) {
                return cache.get(url);
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {
                cache.put(url,bitmap);
            }
        });
    }

    public static VolleyService getInstance(Context context) {
        if (instance == null) {
            instance = new VolleyService(context);
        }
        return instance;
    }

    public RequestQueue getRequestQueue() {
        return requestQueue;
    }

    public ImageLoader getImageLoader() {
        return imageLoader;
    }
}

关于android - Volley 库 vs Android SyncAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30573459/

相关文章:

android - NetworkImageView 的双指缩放

Android Volley 请求身份 onErrorResponse 部分

android - 使用 TensorFlow Android 演示构建错误

android - 如何自定义 ListView 行的外观

android - 有没有办法在通话终止时收到通知

针对 I/O 密集型和 CPU 密集型操作的 Java 线程同步

java - 使用多线程在java中创建骰子游戏

java - 如何使 Logo 在 ActionBar 中居中?

asp.net - ASP.NET Web 服务中的静态字段

php - 使用 android volley 和 php 将波斯语值发布到 mysql