我使用的是包含图片的URL文件名的以下json字符串:
{
"images": [
{
"image_url": "https:\/\/placeimg.com\/150\/50\/nature",
"image_filename": "placeimg_150_50_nature.jpg"
},
{
"image_url": "https:\/\/placeimg.com\/150\/50\/nature",
"image_filename": "placeimg_150_50_nature.jpg"
}
]
}
我也在使用此功能将图像下载到我的SD卡中,该卡完美运行:
public void downloadFile(String uRl, String fileName) {
File file = new File(getAppRootDir()
+ "/images", fileName);
File direct = new File(getAppRootDir()
+ "/images");
if (!direct.exists()) {
direct.mkdirs();
}
if (!file.exists()) {
DownloadManager mgr = (DownloadManager) MainActivity.this.getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse(uRl);
DownloadManager.Request request = new DownloadManager.Request(
downloadUri);
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false).setTitle("Demo")
.setDescription("Something useful. No, really.")
.setDestinationInExternalPublicDir(getAppRootDir() + "/images", fileName);
mgr.enqueue(request);
}
}
我如何将JSON循环到每个条目的get image_url和image_filename?
答案 0 :(得分:1)
响应为JSONObject
,内容为JSONArray
,其中JSONObject的。{1}}的密钥images
包含两个密钥。
获取JSONObject
和image_url
个键值:
image_filename
答案 1 :(得分:0)
您可以将Gson库与http://www.jsonschema2pojo.org/结合使用,以轻松创建解析类。 会很棒。