我有一个 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 不能从我的资产文件夹加载我的 JSON 文件,而它可以从我的网络服务器加载。有没有办法解决这个问题。
任何帮助表示赞赏!