public static void main(String[] args) throws IOException { if (args.length != 2) { System.out.println("Usage: Crawler <cache dir> <root>"); return; } int threadCount = 20; long cacheByteCount = 1024L * 1024L * 100L; OkHttpClient client = new OkHttpClient(); HttpResponseCache httpResponseCache = new HttpResponseCache(new File(args[0]), cacheByteCount); client.setOkResponseCache(httpResponseCache); Crawler crawler = new Crawler(client); crawler.queue.add(new URL(args[1])); crawler.parallelDrainQueue(threadCount); }
@Test public void shouldUse10MegResponseCache() throws Exception { Map map = mapFragment.getMap(); TileLayer baseLayer = field("mBaseLayer").ofType(TileLayer.class).in(map).get(); UrlTileSource tileSource = (UrlTileSource) field("mTileSource"). ofType(TileSource.class).in(baseLayer).get(); HttpEngine.Factory engine = field("mHttpFactory"). ofType(HttpEngine.Factory.class).in(tileSource).get(); OkHttpClient client = field("mClient").ofType(OkHttpClient.class).in(engine).get(); HttpResponseCache cache = (HttpResponseCache) field("responseCache"). ofType(OkResponseCache.class).in(client).get(); assertThat(cache.getMaxSize()).isEqualTo(MapFragment.CACHE_SIZE); }
@Test public void shouldUseResponseCacheStoredOnFile() throws Exception { Map map = mapFragment.getMap(); TileLayer baseLayer = field("mBaseLayer").ofType(TileLayer.class).in(map).get(); UrlTileSource tileSource = (UrlTileSource) field("mTileSource"). ofType(TileSource.class).in(baseLayer).get(); HttpEngine.Factory engine = field("mHttpFactory"). ofType(HttpEngine.Factory.class).in(tileSource).get(); OkHttpClient client = field("mClient").ofType(OkHttpClient.class).in(engine).get(); HttpResponseCache cache = (HttpResponseCache) field("responseCache"). ofType(OkResponseCache.class).in(client).get(); assertThat(cache.getDirectory().getAbsolutePath()). isEqualTo(activity.getExternalCacheDir().getAbsolutePath() + "/tile-cache"); }
@Before public void setUp() throws Exception { String tmp = System.getProperty("java.io.tmpdir"); File cacheDir = new File(tmp, "HttpCache-" + UUID.randomUUID()); cache = new HttpResponseCache(cacheDir, Integer.MAX_VALUE); ResponseCache.setDefault(cache); CookieHandler.setDefault(cookieManager); server.setNpnEnabled(false); }
@Before public void setUp() throws Exception { server.useHttps(sslContext.getSocketFactory(), false); client.setProtocols(Arrays.asList(protocol, Protocol.HTTP_11)); client.setSslSocketFactory(sslContext.getSocketFactory()); client.setHostnameVerifier(NULL_HOSTNAME_VERIFIER); String systemTmpDir = System.getProperty("java.io.tmpdir"); File cacheDir = new File(systemTmpDir, "HttpCache-" + protocol + "-" + UUID.randomUUID()); cache = new HttpResponseCache(cacheDir, Integer.MAX_VALUE); }
static OkHttpClient createOkHttpClient(Application app) { OkHttpClient client = new OkHttpClient(); // Install an HTTP cache in the application cache directory. try { File cacheDir = new File(app.getCacheDir(), "http"); HttpResponseCache cache = new HttpResponseCache(cacheDir, DISK_CACHE_SIZE); client.setResponseCache(cache); } catch (IOException e) { Timber.e(e, "Unable to install disk cache."); } return client; }
public void setupCache(Context context) { try { File httpCacheDirectory = new File(context.getCacheDir().getAbsolutePath(), "HttpCache"); HttpResponseCache cache = new HttpResponseCache(httpCacheDirectory, 20 * 1024); client.setResponseCache(cache); } catch (IOException e) { e.printStackTrace(); } }
private void init() { final File cacheDir = new File(context.getExternalCacheDir().getAbsolutePath() + "/assets"); final int cacheSize = 1024 * 1024; try { cache = new HttpResponseCache(cacheDir, cacheSize); client = new OkHttpClient(); client.setOkResponseCache(cache); } catch (IOException ioe) { Logger.e("network failed: " + ioe.toString()); } }
@Test public void create_shouldUseTileSourceCache() throws Exception { cache = new HttpResponseCache(new File("tmp"), 1024); OSciMap4TileSource tileSource = new OSciMap4TileSource(server.getUrl("/tiles/vtm").toString()); engine = (OkHttpEngine) new OkHttpEngine.OkHttpFactory(cache).create(tileSource); engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue())); engine.requestCompleted(true); assertThat(cache.getRequestCount()).isEqualTo(1); }
static OkHttpClient createOkHttpClient(Application app) { OkHttpClient client = new OkHttpClient(); // Install an HTTP cache in the application cache directory. try { File cacheDir = new File(app.getCacheDir(), "http"); HttpResponseCache cache = new HttpResponseCache(cacheDir, DISK_CACHE_SIZE); client.setResponseCache(cache); } catch (IOException e) { Ln.e(e, "Unable to install disk cache."); } return client; }
@Before public void setUp() throws Exception { String tmp = System.getProperty("java.io.tmpdir"); File cacheDir = new File(tmp, "HttpCache-" + UUID.randomUUID()); cache = new HttpResponseCache(cacheDir, Integer.MAX_VALUE); ResponseCache.setDefault(cache); CookieHandler.setDefault(cookieManager); }
@Before public void setUp() throws Exception { client.setSslSocketFactory(sslContext.getSocketFactory()); client.setHostnameVerifier(NULL_HOSTNAME_VERIFIER); String systemTmpDir = System.getProperty("java.io.tmpdir"); File cacheDir = new File(systemTmpDir, "HttpCache-" + UUID.randomUUID()); cache = new HttpResponseCache(cacheDir, Integer.MAX_VALUE); }
private void installHttpHandler(long httpCacheSize, File httpCacheDir ){ try{ HttpResponseCache result = new HttpResponseCache(httpCacheDir, httpCacheSize); ResponseCache.setDefault(result); URL.setURLStreamHandlerFactory(new OkHttpURLStreamHandlerFactory(new OkHttpClient())); }catch(Exception e){ Ln.e(e, "Failed to set up okhttp"); } }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); try { client = new OkHttpClient(); client.setResponseCache(new HttpResponseCache(getCacheDir(), MAX_CACHE_SIZE)); } catch (IOException e) { e.printStackTrace(); } ViewPager viewPager = new ViewPager(this); viewPager.setId(android.R.id.primary); PagerAdapter adapter = new FragmentAdapter(getSupportFragmentManager()); viewPager.setAdapter(adapter); setContentView(viewPager); mSystemUiHider = SystemUiHider.getInstance(this, viewPager, SystemUiHider.FLAG_HIDE_NAVIGATION); mSystemUiHider.setup(); mSystemUiHider .setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() { int mShortAnimTime; @Override @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2) public void onVisibilityChange(boolean visible) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { if (mShortAnimTime == 0) { mShortAnimTime = getResources().getInteger( android.R.integer.config_shortAnimTime); } } if (visible) { delayedHide(AUTO_HIDE_DELAY_MILLIS); } } }); viewPager.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mSystemUiHider.toggle(); } }); }
private void initResponseCache() throws IOException { String tmp = System.getProperty("java.io.tmpdir"); File cacheDir = new File(tmp, "HttpCache-" + UUID.randomUUID()); cache = new HttpResponseCache(cacheDir, Integer.MAX_VALUE); client.setOkResponseCache(cache); }
public OkHttpFactory(HttpResponseCache responseCache) { mClient = new OkHttpClient(); mClient.setResponseCache(responseCache); }
private void initResponseCache() throws IOException { String tmp = System.getProperty("java.io.tmpdir"); File cacheDir = new File(tmp, "HttpCache-" + UUID.randomUUID()); cache = new HttpResponseCache(cacheDir, Integer.MAX_VALUE); client.setResponseCache(cache); }