com.android.volley.toolbox.DiskBasedCache Java Examples

The following examples show how to use com.android.volley.toolbox.DiskBasedCache. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CloudBackendFragment.java    From io2014-codelabs with Apache License 2.0 6 votes vote down vote up
private RequestQueue newRequestQueue(Context context) {
    // define cache folder
    File rootCache = context.getExternalCacheDir();
    if (rootCache == null) {
        rootCache = context.getCacheDir();
    }

    File cacheDir = new File(rootCache, DEFAULT_CACHE_DIR);
    cacheDir.mkdirs();

    HttpStack stack = new HurlStack();
    Network network = new BasicNetwork(stack);
    DiskBasedCache diskBasedCache = new DiskBasedCache(cacheDir, DEFAULT_DISK_USAGE_BYTES);
    RequestQueue queue = new RequestQueue(diskBasedCache, network);
    queue.start();

    return queue;
}
 
Example #2
Source File: VolleyBallConfig.java    From Volley-Ball with MIT License 6 votes vote down vote up
public VolleyBallConfig build() {
    if (mInstance.mHttpStack == null) {
        mInstance.mHttpStack = ConfigUtils.getDefaultHttpStack(mInstance.mContext);
    }

    if (mInstance.mNetwork == null) {
        mInstance.mNetwork = ConfigUtils.getDefaultNetwork(mInstance.mHttpStack);
    }

    if (mInstance.mCache == null) {
        File cacheDir = new File(mInstance.mContext.getCacheDir(), DEFAULT_CACHE_DIR);
        mInstance.mCache = new DiskBasedCache(cacheDir);
    }

    return mInstance;
}
 
Example #3
Source File: LimitingRequestQueue.java    From odyssey with GNU General Public License v3.0 5 votes vote down vote up
public synchronized static LimitingRequestQueue getInstance(Context context) {
    if (null == mInstance) {
        Network network = new BasicNetwork(new HurlStack());
        // 10MB disk cache
        Cache cache = new DiskBasedCache(context.getCacheDir(), 1024 * 1024 * 10);

        mInstance = new LimitingRequestQueue(cache, network);
        mInstance.start();
    }
    return mInstance;
}
 
Example #4
Source File: DataManager.java    From BigApp_WordPress_Android with Apache License 2.0 5 votes vote down vote up
public static boolean clearUserInfo(Context context) {
    File file_collect = new DiskBasedCache(SdCacheTools.getOwnVolleyCacheDir(context)).getFileForKey(RequestUrl.my_collect + "&filter[pre_page]=" + 10 + "&page=" + 1);
    File file_commit = new DiskBasedCache(SdCacheTools.getOwnVolleyCacheDir(context)).getFileForKey(RequestUrl.my_commit + "&filter[pre_page]=" + 10 + "&page=" + 1);
    Log.e("", "wenjun logout clearUserInfo collect file length = " + (file_collect == null ? 0 : file_collect.length()) + "&  commitfile length = " + (file_commit == null ? 0 : file_commit.length()));
    file_collect.delete();
    file_commit.delete();
    return true;
}
 
Example #5
Source File: ImageLoader.java    From android-discourse with Apache License 2.0 5 votes vote down vote up
private static RequestQueue newRequestQueue(Context context) {

        // On HC+ use HurlStack which is based on HttpURLConnection. Otherwise fall back on
        // AndroidHttpClient (based on Apache DefaultHttpClient) which should no longer be used
        // on newer platform versions where HttpURLConnection is simply better.
        Network network = new BasicNetwork(Utils.hasHoneycomb() ? new HurlStack() : new HttpClientStack(AndroidHttpClient.newInstance(Utils.getUserAgent(context))));

        Cache cache = new DiskBasedCache(getDiskCacheDir(context, CACHE_DIR), DEFAULT_DISK_USAGE_BYTES);
        RequestQueue queue = new RequestQueue(cache, network);
        queue.start();
        return queue;
    }
 
Example #6
Source File: WaspTest.java    From wasp with Apache License 2.0 5 votes vote down vote up
public WaspTest() throws Exception {
  File cacheDir = new File(context.getCacheDir(), "volley");
  Network network = new BasicNetwork(new OkHttpStack(new OkHttpClient()));
  ResponseDelivery delivery = new ExecutorDelivery(executor);
  requestQueue = new RequestQueue(new DiskBasedCache(cacheDir), network, 4, delivery);
  requestQueue.start();

  server.start();
}
 
Example #7
Source File: VolleyTestMainActivity.java    From android_tv_metro with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.test_main);

	txtDisplay = (TextView) findViewById(R.id.txtDisplay);

	// Instantiate the cache
	Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB cap
	// Set up the network to use HttpURLConnection as the HTTP client.
	Network network = new BasicNetwork(new HurlStack());
	// Instantiate the RequestQueue with the cache and network.
	mRequestQueue = new RequestQueue(cache, network);
	// Start the queue
	mRequestQueue.start();
	
	String url = "http://172.27.9.104:9300/testdata/1/1/1/zh/CN?api=index";

	JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null,
			new Response.Listener<JSONObject>() {

				@Override
				public void onResponse(JSONObject response) {
					// TODO Auto-generated method stub
					txtDisplay.setText("Response => " + response.toString());
					findViewById(R.id.progressBar1).setVisibility(View.GONE);
				}
			}, new Response.ErrorListener() {

				@Override
				public void onErrorResponse(VolleyError error) {
					txtDisplay.setText("VolleyError => " + error.toString());
					findViewById(R.id.progressBar1).setVisibility(View.GONE);
				}
			});

	mRequestQueue.add(jsObjRequest);

}
 
Example #8
Source File: GetChromium.java    From getChromium with GNU General Public License v3.0 4 votes vote down vote up
private void downloadLatest() {

        // Instantiate the cache
        Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB cap

        // Set up the network to use HttpURLConnection as the HTTP client.
        com.android.volley.Network network = new BasicNetwork(new HurlStack());

        // Instantiate the RequestQueue with the cache and network.
        mRequestQueue = new RequestQueue(cache, network);
        mRequestQueue.start();

        // Request a string response from "https://commondatastorage.googleapis.com/chromium-browser-snapshots/Android/LAST_CHANGE"
        // The response provides the current build number of Chromium's latest build
        String urlL = "https://commondatastorage.googleapis.com/chromium-browser-snapshots/Android/LAST_CHANGE";
        final StringRequest stringRequest = new StringRequest(Request.Method.GET, urlL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        // Build new String for the current APK download URL
                        Uri.Builder builder = new Uri.Builder();
                        builder.scheme("https")
                                .authority("commondatastorage.googleapis.com")
                                .appendPath("chromium-browser-snapshots")
                                .appendPath("Android")
                                .appendPath(response) // Build revision number
                                .appendPath("chrome-android.zip"); // Name of the file that will contain the APKs.
                        String apkUrl = builder.build().toString();
                        new DownloadTask().execute(apkUrl);
                    }
                },

                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                    }
                });

        // Add the request to the RequestQueue.
        mRequestQueue.add(stringRequest);
    }
 
Example #9
Source File: FadeInNetworkImageView.java    From QuickLyric with GNU General Public License v3.0 4 votes vote down vote up
public FadeInNetworkImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    File cacheDir = new File(getContext().getCacheDir(), "volley");
    mDiskCache = new DiskBasedCache(cacheDir);
    new Thread(mDiskCache::initialize).start();
}
 
Example #10
Source File: DefaultCrossbowComponents.java    From CrossBow with Apache License 2.0 4 votes vote down vote up
public Cache onCreateCache() {
    String cacheDir = getContext().getCacheDir() + File.separator + "CrossbowCache";
    File file = new File(cacheDir);
    return new DiskBasedCache(file, DISK_CACHE_SIZE);
}
 
Example #11
Source File: JacksonNetwork.java    From volley-jackson-extension with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it.
 *
 * @param context A {@link Context} to use for creating the cache dir.
 * @param stack An {@link com.android.volley.toolbox.HttpStack} to use for handling network calls
 * @return A started {@link RequestQueue} instance.
 */
public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
	RequestQueue queue = new RequestQueue(new DiskBasedCache(new File(context.getCacheDir(), "volley")), new JacksonNetwork(stack));
	queue.start();
	return queue;
}