在 Kotlin 中将 object 改造为其他 activity
Put retrofit object to other activity in Kotlin
正如我在标题中提到的,我正在尝试将我在 MainActivity 中使用的名为“服务”的 object 发送到 OtherActivity,但我没有成功。
Constant.kt
class Constants {
companion object{
const val BASE_URL = "http://10.0.2.2:8080/customer/"
}
}
RetrofitInstance.kt
object RetrofitInstance {
private var retrofit: Retrofit? = null
fun getClient(username: String, password : String): Retrofit {
val client = OkHttpClient.Builder()
.addInterceptor(BasicAuthInterceptor(username,password))
.build()
val gson = GsonBuilder().apply {
setLenient()
registerTypeAdapter(Date::class.java,
JsonDeserializer<Date> { json, typeOfT, context ->
if(json.asJsonPrimitive.isNumber) {
Date(json.asJsonPrimitive.asLong * 1000)
} else {
null
}
})
}.create()
if (retrofit == null)
retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
return retrofit as Retrofit
}
}
SimpleApi.kt
interface SimpleApi : Serializable{
@GET("list")
fun productList(): Call<MutableList<Product>>
@GET("api/data")
fun test(@Header("Authorization") autHeader: String): Call<String>
@GET("testc")
fun testCustomer(): Call<String>
@GET("cars")
fun getCars(): Call<List<Car>>
}
在 MainActivity.kt
中创建“服务”
val service = RetrofitInstance.getClient(username,password).create(SimpleApi::class.java)
在 MainActivity.kt
中创建“意图”
val intent = Intent(this@MainActivity,OtherActivity::class.java)
intent.putExtra("service", service as Serializable)
startActivity(intent)
在 OtherActivity.kt
中获取“意图”
val service = (intent.getSerializableExtra("service") as? SimpleApi)!!
您可以像下面这样更改您的RetrofitInstance
object RetrofitInstance {
private var api: SimpleApi? = null
fun createInstance(username: String, password: String): SimpleApi {
val client = OkHttpClient.Builder()
.addInterceptor(BasicAuthInterceptor(username, password))
.build()
val gson = GsonBuilder().apply {
setLenient()
registerTypeAdapter(Date::class.java,
JsonDeserializer<Date> { json, typeOfT, context ->
if (json.asJsonPrimitive.isNumber) {
Date(json.asJsonPrimitive.asLong * 1000)
} else {
null
}
})
}.create()
if (api == null)
api = Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
.create(SimpleApi::class.java)
return api
}
fun getInstance() : SimpleApi {
return api!!
}
}
在您的 MainActivity
上调用 createInstace
函数来创建您的 api 实例。其他活动可以调用RetrofitInstance.getInstance()
得到api.
正如我在标题中提到的,我正在尝试将我在 MainActivity 中使用的名为“服务”的 object 发送到 OtherActivity,但我没有成功。
Constant.kt
class Constants {
companion object{
const val BASE_URL = "http://10.0.2.2:8080/customer/"
}
}
RetrofitInstance.kt
object RetrofitInstance {
private var retrofit: Retrofit? = null
fun getClient(username: String, password : String): Retrofit {
val client = OkHttpClient.Builder()
.addInterceptor(BasicAuthInterceptor(username,password))
.build()
val gson = GsonBuilder().apply {
setLenient()
registerTypeAdapter(Date::class.java,
JsonDeserializer<Date> { json, typeOfT, context ->
if(json.asJsonPrimitive.isNumber) {
Date(json.asJsonPrimitive.asLong * 1000)
} else {
null
}
})
}.create()
if (retrofit == null)
retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
return retrofit as Retrofit
}
}
SimpleApi.kt
interface SimpleApi : Serializable{
@GET("list")
fun productList(): Call<MutableList<Product>>
@GET("api/data")
fun test(@Header("Authorization") autHeader: String): Call<String>
@GET("testc")
fun testCustomer(): Call<String>
@GET("cars")
fun getCars(): Call<List<Car>>
}
在 MainActivity.kt
中创建“服务” val service = RetrofitInstance.getClient(username,password).create(SimpleApi::class.java)
在 MainActivity.kt
中创建“意图” val intent = Intent(this@MainActivity,OtherActivity::class.java)
intent.putExtra("service", service as Serializable)
startActivity(intent)
在 OtherActivity.kt
中获取“意图”val service = (intent.getSerializableExtra("service") as? SimpleApi)!!
您可以像下面这样更改您的RetrofitInstance
object RetrofitInstance {
private var api: SimpleApi? = null
fun createInstance(username: String, password: String): SimpleApi {
val client = OkHttpClient.Builder()
.addInterceptor(BasicAuthInterceptor(username, password))
.build()
val gson = GsonBuilder().apply {
setLenient()
registerTypeAdapter(Date::class.java,
JsonDeserializer<Date> { json, typeOfT, context ->
if (json.asJsonPrimitive.isNumber) {
Date(json.asJsonPrimitive.asLong * 1000)
} else {
null
}
})
}.create()
if (api == null)
api = Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
.create(SimpleApi::class.java)
return api
}
fun getInstance() : SimpleApi {
return api!!
}
}
在您的 MainActivity
上调用 createInstace
函数来创建您的 api 实例。其他活动可以调用RetrofitInstance.getInstance()
得到api.