[英]Difference between file://android_assets/ and http://someurl.com/?
我有一個android應用,可從網絡上的JSON文件加載圖像。 JSON文件中包含一個URL,其中包含圖像上的位置。 通常,該應用會讀取JSON文件以從網絡廣告下載圖像。 但是現在我想從我的資產文件夾中下載文件,並讀取我的資產目錄中的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無法從我的Assets文件夾加載我的JSON文件,而卻可以從我的Web服務器加載它。 並且有辦法解決這個問題。
任何幫助表示贊賞!
file:///代表一個不同於HTTP URI的URI,它指向資產目錄中包含的數據。 您的下載方法顯然是為HTTP URL設計的。
為了從資產目錄中加載數據,您可以使用AssetManager ,尤其可以使用open方法。
例如:
AssetManager assetMgr = this.getAssets()
InputStream is = assetMgr.open("1.jpg");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.