从OSMBonuspack使用CacheManager.downloadAreaAsync的问题

时间:2015-11-18 08:30:41

标签: android osmdroid

我只是在编写一个应用程序,该应用程序应该能够预取Maptiles以在没有数据连接的地方使用该应用程序。出于这个原因,我使用Open Street Maps和我的Android客户端OSMDroid和OSMdroid Bonuspack。为了生产,我将使用自己的OSM服务器。

这是我下载已定义区域的图块的代码:

map.setTileSource(TileSourceFactory.MAPQUESTOSM);
map.setBuiltInZoomControls(true);
map.setMultiTouchControls(true);

BoundingBoxE6 boxE6 = new BoundingBoxE6(51.758971, 7.100778, 50.653902, 6.689312);
CacheManager cacheManager = new CacheManager(map);
cacheManager.downloadAreaAsync(getActivity(), boxE6, 13, 15);

我的build.gradle包含所有需要的依赖项,根据OSMdroid Bonuspack的GitHub页面(当然没有构建错误):

compile 'org.osmdroid:osmdroid-android:5.0.1@aar'
compile 'org.apache.commons:commons-lang3:3.3.2'
compile 'com.google.code.gson:gson:2.3'
compile project(':osmbonuspack_v5.4')

当我运行代码时,会出现对话框并显示每个缩放级别下载切片的进度。但我的logcat充斥着以下错误:

Error downloading MapTile: /15/17015/11010
java.lang.NoClassDefFoundError: org.osmdroid.http.HttpClientFactory
at org.osmdroid.bonuspack.cachemanager.CacheManager.loadTile(CacheManager.java:95)
at org.osmdroid.bonuspack.cachemanager.CacheManager$DownloadingTask.downloadArea(CacheManager.java:259)
at org.osmdroid.bonuspack.cachemanager.CacheManager$DownloadingTask.doInBackground(CacheManager.java:230)
at org.osmdroid.bonuspack.cachemanager.CacheManager$DownloadingTask.doInBackground(CacheManager.java:207)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)

Proguard被禁用。

2 个答案:

答案 0 :(得分:0)

由于我无法找到,为什么缺少org.osmdroid.http.HttpClientFactory,我创建了FixedCacheManager并覆盖了loadTile()。在这个方法中,我使用OkHttp下载磁贴:

@Override
    public boolean loadTile(OnlineTileSourceBase tileSource, MapTile tile) {
        File file = this.getFileName(tileSource, tile);
        if(file.exists()) {
            return true;
        } else {
            InputStream in = null;
            BufferedOutputStream out = null;

            boolean var15;
            try {
                String e = tileSource.getTileURLString(tile);
                Request request = new Request.Builder().get().url(e).build();
                Response response = client.newCall(request).execute();

                if(!response.isSuccessful()) {
                    Log.w("BONUSPACK", "Problem downloading MapTile: " + tile + " HTTP response: " + response.code());
                    boolean entity1 = false;
                    return entity1;
                }

                final ResponseBody body = response.body();
                if(body == null) {
                    Log.w("BONUSPACK", "No content downloading MapTile: " + tile);
                    boolean dataStream1 = false;
                    return dataStream1;
                }

                in = body.byteStream();
                ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
                out = new BufferedOutputStream(dataStream, 8192);
                StreamUtils.copy(in, out);
                out.flush();
                byte[] data = dataStream.toByteArray();
                ByteArrayInputStream byteStream = new ByteArrayInputStream(data);
                this.mTileWriter.saveFile(tileSource, tile, byteStream);
                byteStream.reset();
                var15 = true;
            } catch (UnknownHostException var22) {
                Log.w("BONUSPACK", "UnknownHostException downloading MapTile: " + tile + " : " + var22);
                return false;
            } catch (FileNotFoundException var23) {
                Log.w("BONUSPACK", "Tile not found: " + tile + " : " + var23);
                return false;
            } catch (IOException var24) {
                Log.w("BONUSPACK", "IOException downloading MapTile: " + tile + " : " + var24);
                return false;
            } catch (Throwable var25) {
                Log.e("BONUSPACK", "Error downloading MapTile: " + tile, var25);
                return false;
            } finally {
                StreamUtils.closeStream(in);
                StreamUtils.closeStream(out);
            }

            return var15;
        }
    }

答案 1 :(得分:0)

答案:你的osmdroid和osmbonus包装不匹配。

osmbonuspack 5.5需要osmdroid 5.0.1。

原因:osmdroid 5.x支持更新的android apis(或缺少它)。 osmbonuspack也更新以支持这些变化....在v5.5