我有一個android應用程序,它從Web上的JSON文件加載圖像。 JSON文件在其中包含一個帶有圖像位置的URL。通常,應用程序讀取JSON文件以從Web地址下載圖像。但現在我想從我的資產文件夾下載文件,並閱讀資產目錄中的JSON文件(我正在使用file:// android_assets)。file:// android_assets /和http://someurl.com/之間的區別?
我的第一個問題是,爲什麼不能讀取我的資產文件夾中的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)
@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是文件://android_assets/myfile.json
現在我想知道,爲什麼不能從的android我的資產文件夾加載我JSON文件我的代碼從服務器下載JSON ,雖然它可以從我的網絡服務器。有沒有辦法解決這個問題。
任何幫助表示讚賞!
請添加像什麼的下載功能沒有,也請添加更多的logcat數據 – 2013-04-29 20:00:05
更新:)更多的細節,感謝您嘗試幫助我! – Mdlc 2013-04-29 20:03:42
請添加this.download函數,它會告訴代碼的哪部分代碼會生成錯誤。 – 2013-04-29 20:10:14