Download & Cache images,json,xml etc... in memory for Android
implementation(project(':mvload')) {
exclude group: 'com.android.support'
}
val imageView:ImageView = ......
...
imageView.load(url = ".......",placeHolder = R.drawable.placeholder,errorImage = R.drawable.error_place_holder)
val client: MVLoadClient= MVLoadClient()
client.requestAsJsonObject("http://pastebin.com/raw/wgkJgazE")
{ jsonObject: JSONObject?, throwable: Throwable? ->
Log.d(TAG, jsonObject?.toString())
}
val client: MVLoadClient= MVLoadClient()
client.requestAsJsonArray("http://pastebin.com/raw/wgkJgazE")
{ jsonArray: JSONArray?, throwable: Throwable? ->
Log.d(TAG, jsonObject?.toString())
}
1- First , you should implement StreamMapper to serialize/deserialize the data for ByteArray to what you need in this example we will use Gson to convert the bytes to list of objects
val mapper= object : StreamMapper<ByteArray, List<PinterestItem>> {
override fun map(input: ByteArray): List<PinterestItem> {
return Gson().fromJson(String(input),
Array<PinterestItem>::class.java).toList()
}
}
2- Then...
val client: MVLoadClient= MVLoadClient()
client.requestAsGeneric("http://pastebin.com/raw/wgkJgazE",
mapper = mapper){ list: List<PinterestItem>?, throwable: Throwable? ->
...
}
Add the initialization in your application class
val cacheConfig= CacheConfig(cacheSize = 100,inMemoryExpiration = 6000)
MVLoad.init(cacheConfig)
This is an Android Studio project using the Gradle build system, to build this project you need
- Build tools version 28.0.0-alpha3
- API level 28
- Android Studio 3
- Gradle Version 3