0
votes

I am just writing an app which should be able to prefetch Maptiles to use the app in places where no data connection is available. For this reason I use Open Street Maps and on my Android client OSMDroid and OSMdroid Bonuspack. For production I will use my own OSM server.

This is my code for downloading the tiles of a defined area:

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);

My build.gradle contains all dependencies which are needed, according to the GitHub page of OSMdroid Bonuspack (of course no build errors):

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')

When I run my code, the dialog appears and shows the progress of downloading the tiles for each zoom level. But my logcat flooded with the following error:

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 is disabled.

2
Which versions of osmdroid and OSMBonusPack are you using? - MKer

2 Answers

0
votes

As I was not able to find out, why org.osmdroid.http.HttpClientFactory is missing, I have created FixedCacheManager and overrode loadTile(). Inside this method I use OkHttp to download the tiles:

@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;
        }
    }
0
votes

Answer: you have mismatched osmdroid and osmbonus packs.

You need osmdroid 5.0.1 with osmbonuspack 5.5.

Reason: osmdroid 5.x supports the newer android apis (or lack thereof). osmbonuspack was also updated to support those changes.... in v5.5