file:// android_assets /和http://someurl.com/之间的区别?

时间:2013-04-29 19:56:59

标签: android

我有一个Android应用程序,可以从Web上的JSON文件加载图像。 JSON文件中包含一个URL,其中包含图像上的位置。通常,应用程序会读取JSON文件以从Web地址下载图像。但现在我想从我的assets文件夹下载文件,并读取我的assets目录中的JSON文件(我正在使用file:// android_assets)。

我的第一个问题是,为什么无法从我的资源文件夹中读取json文件?

我的JSON文件,不是真的需要解决我的第一个问题,但我只是发布它:

旧JSON:

[
{
    "id": "1",
    "title": "Blue Calmness",
    "thumburl": "http://someurl.com/1.jpg",
    "previewurl": "http://someurl.com/1.jpg",
    "url": "http://someurl.com/1.jpg",
    "text": "Give your background a special message! An inspirational image, the texture and its calm blue color are stimulating and representing the silent word of the see. For a positive look at the world."
}
]

新JSON:

[
{
    "id": "1",
    "title": "Blue Calmness",
    "thumburl": "file:///android_assets/1.jpg",
    "previewurl": "file:///android_assets/1.jpg",
    "url": "file:///android_assets/1.jpg",
    "text": "Give your background a special message! An inspirational image, the texture and its calm blue color are stimulating and representing the silent word of the see. For a positive look at the world."
}
]

但我收到错误消息:

   04-29 21:52:42.169: E/AndroidRuntime(23408): FATAL EXCEPTION: main
   04-29 21:52:42.169: E/AndroidRuntime(23408): java.lang.RuntimeException: java.lang.IllegalArgumentException: Host name may not be null
   04-29 21:52:42.169: E/AndroidRuntime(23408):     at app.wallpaper.main.ScroidWallpaperGallery$FillGalleryTask.handleUncaughtException(ScroidWallpaperGallery.java:415)
   04-29 21:52:42.169: E/AndroidRuntime(23408):     at app.wallpaper.util.threading.LongTimeRunningOperation$InnerAsyncTask.onPostExecute(LongTimeRunningOperation.java:87)
   04-29 21:52:42.169: E/AndroidRuntime(23408):     at android.os.AsyncTask.finish(AsyncTask.java:631)
   04-29 21:52:42.169: E/AndroidRuntime(23408):     at android.os.AsyncTask.access              $600(AsyncTask.java:177)
   04-29 21:52:42.169: E/AndroidRuntime(23408):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
   04-29 21:52:42.169: E/AndroidRuntime(23408):     at android.os.Handler.dispatchMessage(Handler.java:99)
   04-29 21:52:42.169: E/AndroidRuntime(23408):     at android.os.Looper.loop(Looper.java:137)
   04-29 21:52:42.169: E/AndroidRuntime(23408):     at android.app.ActivityThread.main(ActivityThread.java:5041)
   04-29 21:52:42.169: E/AndroidRuntime(23408):     at java.lang.reflect.Method.invokeNative(Native Method)
   04-29 21:52:42.169: E/AndroidRuntime(23408):     at java.lang.reflect.Method.invoke(Method.java:511)
   04-29 21:52:42.169: E/AndroidRuntime(23408):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
   04-29 21:52:42.169: E/AndroidRuntime(23408):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
   04-29 21:52:42.169: E/AndroidRuntime(23408):     at dalvik.system.NativeStart.main(Native Method)
   04-29 21:52:42.169: E/AndroidRuntime(23408): Caused by: java.lang.IllegalArgumentException: Host name may not be null
   04-29 21:52:42.169: E/AndroidRuntime(23408):     at org.apache.http.HttpHost.<init>(HttpHost.java:83)
   04-29 21:52:42.169: E/AndroidRuntime(23408):     at org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:497)
   04-29 21:52:42.169: E/AndroidRuntime(23408):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:626)
   04-29 21:52:42.169: E/AndroidRuntime(23408):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
   04-29 21:52:42.169: E/AndroidRuntime(23408):     at app.wallpaper.main.dao.wallpapers.WallpaperDAO.download(WallpaperDAO.java:105)
   04-29 21:52:42.169: E/AndroidRuntime(23408):     at app.wallpaper.main.dao.wallpapers.WallpaperDAO.getAvailableWallpapers(WallpaperDAO.java:61)
   04-29 21:52:42.169: E/AndroidRuntime(23408):     at app.wallpaper.main.core.wallpapers.WallpaperManager.loadAvailableWallpapers(WallpaperManager.java:92)
   04-29 21:52:42.169: E/AndroidRuntime(23408):     at app.wallpaper.main.ScroidWallpaperGallery$FillGalleryTask.onRun(ScroidWallpaperGallery.java:425)
   04-29 21:52:42.169: E/AndroidRuntime(23408):     at app.wallpaper.main.ScroidWallpaperGallery$FillGalleryTask.onRun(ScroidWallpaperGallery.java:1)
   04-29 21:52:42.169: E/AndroidRuntime(23408):     at app.wallpaper.util.threading.LongTimeRunningOperation$InnerAsyncTask.doInBackground(LongTimeRunningOperation.java:73)
   04-29 21:52:42.169: E/AndroidRuntime(23408):     at app.wallpaper.util.threading.LongTimeRunningOperation$InnerAsyncTask.doInBackground(LongTimeRunningOperation.java:1)
   04-29 21:52:42.169: E/AndroidRuntime(23408):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
   04-29 21:52:42.169: E/AndroidRuntime(23408):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
   04-29 21:52:42.169: E/AndroidRuntime(23408):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
   04-29 21:52:42.169: E/AndroidRuntime(23408):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
   04-29 21:52:42.169: E/AndroidRuntime(23408):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
   04-29 21:52:42.169: E/AndroidRuntime(23408):     at java.lang.Thread.run(Thread.java:856)

我从服务器下载json的代码:

    @Inject
WallpaperDAO(IWallpaperParser wallpaperParser) {
    super();

    this.wallpaperParser = wallpaperParser;
}


/* (non-Javadoc)
 * @see app.wallpaper.main.dao.wallpapers.IWallpaperDAO#getAvailableWallpapers(android.content.Context)
 */
public Wallpaper[] getAvailableWallpapers(Context context) throws WallpaperListReceivingException {     
    try {
        String data = this.download(URI.create(context.getString(R.string.galleryUrl)), 
                                    new TextFileHttpResponseHandler(),
                                    MAX_DOWNLOAD_RETRIES);

        List<Wallpaper> wallpapers = this.wallpaperParser.parse(data);

        return wallpapers.toArray(new Wallpaper[wallpapers.size()]);
    }
    catch (IllegalStateException ex) {
        throw new WallpaperListReceivingException(context.getString(R.string.receivingException), ex);
    }
    catch (IOException ex) {
        throw new WallpaperListReceivingException(context.getString(R.string.receivingException), ex);
    }
    catch (ParseException ex) {
        throw new WallpaperListReceivingException(context.getString(R.string.parseExceptionText), ex);
    }
}

其中galleryUrl是file://android_assets/myfile.json

现在我想知道,为什么Android不能从我的资源文件夹加载我的JSON文件,而它可以从我的网络服务器加载。有没有办法解决这个问题。

感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

file:/// 表示与HTTP URI不同的URI,该URI指向资产目录中包含的数据。您的下载方法明确是为HTTP URL设计的。

要从资源目录加载数据,您可以特别使用AssetManageropen方法。

例如:

AssetManager assetMgr = this.getAssets()
InputStream is = assetMgr.open("1.jpg");