因此,我将从服务器获得如下JSON:
{
"success": "1",
"data": [
{
"id": 1,
"title": "1",
"url": "5",
"image": "resources/assets/images/banner_images/1548316546.mitra lakuin.png",
"type": "category"
},
{
"id": 2,
"title": "2",
"url": "5",
"image": "resources/assets/images/banner_images/1548316605.mobile banner_-02 (1).png",
"type": "category"
},
{
"id": 4,
"title": "4",
"url": "5",
"image": "resources/assets/images/banner_images/1547010221.gratis ongkir.png",
"type": "category"
},
{
"id": 5,
"title": "Customer",
"url": "5",
"image": "resources/assets/images/banner_images/1548316659.customer service.png",
"type": "category"
}
],
"message": "Banners are returned successfull."
}
我创建了2个类,以便将JSON转换为Banner对象。
// 1.
class BannerData(val message : String, val success : String, val data : List<Banner>) {}
// 2.
class Banner(id: Int, imageURL: String) {
@SerializedName("image")
var imageURL : String
val id : Int
init {
Log.i("testX","triggered")
this.id = id
// The code below is used to add percent encoding to URL from server
val unformattedFullPath = "${Endpoint.lakuin}/$imageURL"
val formattedFullPath = URLConverterService.addingPercentEncoding(unformattedFullPath)
this.imageURL = formattedFullPath
}
}
改装界面如下所示:
@GET("/app/getbanners")
fun getBanners(): Call<BannerData>
object RetrofitHandler {
private val loggingInterceptor = HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
private val okHttpClient = OkHttpClient.Builder()
.callTimeout(7,TimeUnit.SECONDS)
.addInterceptor(loggingInterceptor)
.build()
private var retrofit = Retrofit.Builder()
.baseUrl(Endpoint.lakuin) // default is set to lakuinAPI
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build()
fun <T> getInstance(APIType: Class<T>) : T {
// baseURL should be change later, if there is more than one API, ex: GoogleMapAPI, LakuinAPI etc
return retrofit.create(APIType)
}
}
我的问题是。。。。。
从上面的JSON可以看出,图像URL不完整,我想转换图像URL
from:“resources/bla.png”
成为:
http://xxxx.com/resources/blablabla.png
"
Banner
班
但是它似乎是应用程序中的init块
横幅
Log.i("testX","triggered")
从不在日志中显示
我实际上可以创建Banner对象,但imageURL与JSON完全相同,这意味着init块永远不会被触发