RetrofitService 没有得到 called/working 并且也没有错误 Android Retrofit, Kotlin, Viewmodel
RetrofitService not getting called/working and has no error either Android Retrofit, Kotlin, Viewmodel
您好,我正在尝试使用改装进行基本 api 调用,并将对象放在 recyclerview 上。下面是 Retrofit Instance, service, viewmodel 的代码。
SearchFragmentViewModel.kt
class SearchFragmentViewModel(application: Application): AndroidViewModel(application){
var eventSearchList: MutableLiveData<ArrayList<EventSearchData>>
// var eventSearchList: MutableLiveData<EventSearchList>
val TAG:String = "demo"``
init {
eventSearchList = MutableLiveData()
//getDataFromApi("")
}
fun getRecyclerListDataObserver(): MutableLiveData<ArrayList<EventSearchData>> {
return eventSearchList
}
fun getDataFromApi(tempString: String) {
val TAG: String = "demo"
viewModelScope.launch(Dispatchers.IO) {
val retroInstance = RetrofitInstance.getRetrofitInstance()
.create(RetrofitService::class.java)
val call = retroInstance.getEventDataFromApi(tempString) //from this point not working
if (call.isExecuted){
Log.d(TAG, "if call is excecuted")
}
call.enqueue(object :retrofit2.Callback<EventSearchList>{
override fun onResponse(call: Call<EventSearchList>, response: Response<EventSearchList>) {
Log.d(TAG, "onResponse: on success")
if (response.isSuccessful){
eventSearchList.postValue(response.body()?.event)
Log.d(TAG, "onResponse: ${response.body()}")
}else{
eventSearchList.postValue(null)
}
}
override fun onFailure(call: Call<EventSearchList>, t: Throwable) {
eventSearchList.postValue(null)
Log.d(TAG, "onFailure: call retrofit") }
})
Log.d(TAG, "onFailure: dfdag refdhdfhtrofit")
}
}
}
RetroInstance.kt
class RetrofitInstance {
companion object{
val BASE_URL ="https://www.thesportsdb.com/api/v1/json/1/"
fun getRetrofitInstance(): Retrofit {
return Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
}
}
RetrofitService.kt
interface RetrofitService {
@GET("searchevents.php")
suspend fun getEventDataFromApi(@Query("e") query: String): Call<EventSearchList>
}
从 viewmodel 中的调用方法,没有得到任何东西,(起初我试图显示 response.body,但没有成功)最终在调试代码后我发现没有 LogCat 得到比点后打印。请让我知道我做错了什么。
P.S - 尝试检查 android:textTraffic="true,
gradle 个用于 retrofit2 库实现的文件,
使用回调和不使用回调,但没有通过 retroservice 调用
查看您的代码,您正在尝试同时使用两种不同的线程工具,但这是行不通的。一个是 coroutines,第二个是 enqueue。如果你想使用 enqueue,你不必在你的界面中暂停你的 GET 乐趣,它看起来像这样
interface RetrofitService {
@GET("searchevents.php") fun getEventDataFromApi(@Query("e") query: String): Call
}
此外,您还必须将您的响应 class 包装在界面中,这样调用
interface RetrofitService {
@GET("searchevents.php") suspend fun getEventDataFromApi(@Query("e") query: String): Call<EventSearchList>
}
如果 EventSearchList 是您预期来自 API 的响应。
因此,不需要 ViewModel 中的协程作用域。你的电话看起来像
fun getDataFromApi(tempString: String) {
val call = retroInstance.getEventDataFromApi(tempString)
call.enqueue(object :retrofit2.Callback<EventSearchList>{
override fun onResponse(call: Call<EventSearchList>, response: Response<EventSearchList>) {
Log.d(TAG, "onResponse: on success")
if (response.isSuccessful){
eventSearchList.postValue(response.body()?.event)
Log.d(TAG, "onResponse: ${response.body()}")
}else{
eventSearchList.postValue(null)
}
}
override fun onFailure(call: Call<EventSearchList>, t: Throwable) {
eventSearchList.postValue(null)
Log.d(TAG, "onFailure: call retrofit") }
})
}
另外,不知道你的retrofit2是什么意思。我想你会更好地理解。
我还会建议您将 OKHTTP 拦截器 https://blog.mindorks.com/okhttp-interceptor-making-the-most-of-it 添加到您的改造中,以了解您的网络调用有什么问题
class RetrofitInstance {
companion object{
val BASE_URL ="https://www.thesportsdb.com/api/v1/json/1/"
fun getRetrofitInstance(): Retrofit {
return Retrofit.Builder()
.client(OkHttpClient.Builder().addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)).build())
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
}
对于协程,您可以查看此https://blog.mindorks.com/using-retrofit-with-kotlin-coroutines-in-android并将其与您的工作联系起来
您好,我正在尝试使用改装进行基本 api 调用,并将对象放在 recyclerview 上。下面是 Retrofit Instance, service, viewmodel 的代码。
SearchFragmentViewModel.kt
class SearchFragmentViewModel(application: Application): AndroidViewModel(application){
var eventSearchList: MutableLiveData<ArrayList<EventSearchData>>
// var eventSearchList: MutableLiveData<EventSearchList>
val TAG:String = "demo"``
init {
eventSearchList = MutableLiveData()
//getDataFromApi("")
}
fun getRecyclerListDataObserver(): MutableLiveData<ArrayList<EventSearchData>> {
return eventSearchList
}
fun getDataFromApi(tempString: String) {
val TAG: String = "demo"
viewModelScope.launch(Dispatchers.IO) {
val retroInstance = RetrofitInstance.getRetrofitInstance()
.create(RetrofitService::class.java)
val call = retroInstance.getEventDataFromApi(tempString) //from this point not working
if (call.isExecuted){
Log.d(TAG, "if call is excecuted")
}
call.enqueue(object :retrofit2.Callback<EventSearchList>{
override fun onResponse(call: Call<EventSearchList>, response: Response<EventSearchList>) {
Log.d(TAG, "onResponse: on success")
if (response.isSuccessful){
eventSearchList.postValue(response.body()?.event)
Log.d(TAG, "onResponse: ${response.body()}")
}else{
eventSearchList.postValue(null)
}
}
override fun onFailure(call: Call<EventSearchList>, t: Throwable) {
eventSearchList.postValue(null)
Log.d(TAG, "onFailure: call retrofit") }
})
Log.d(TAG, "onFailure: dfdag refdhdfhtrofit")
}
}
}
RetroInstance.kt
class RetrofitInstance {
companion object{
val BASE_URL ="https://www.thesportsdb.com/api/v1/json/1/"
fun getRetrofitInstance(): Retrofit {
return Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
}
}
RetrofitService.kt
interface RetrofitService {
@GET("searchevents.php")
suspend fun getEventDataFromApi(@Query("e") query: String): Call<EventSearchList>
}
从 viewmodel 中的调用方法,没有得到任何东西,(起初我试图显示 response.body,但没有成功)最终在调试代码后我发现没有 LogCat 得到比点后打印。请让我知道我做错了什么。
P.S - 尝试检查 android:textTraffic="true, gradle 个用于 retrofit2 库实现的文件, 使用回调和不使用回调,但没有通过 retroservice 调用
查看您的代码,您正在尝试同时使用两种不同的线程工具,但这是行不通的。一个是 coroutines,第二个是 enqueue。如果你想使用 enqueue,你不必在你的界面中暂停你的 GET 乐趣,它看起来像这样
interface RetrofitService {
@GET("searchevents.php") fun getEventDataFromApi(@Query("e") query: String): Call
}
此外,您还必须将您的响应 class 包装在界面中,这样调用
interface RetrofitService {
@GET("searchevents.php") suspend fun getEventDataFromApi(@Query("e") query: String): Call<EventSearchList>
}
如果 EventSearchList 是您预期来自 API 的响应。
因此,不需要 ViewModel 中的协程作用域。你的电话看起来像
fun getDataFromApi(tempString: String) {
val call = retroInstance.getEventDataFromApi(tempString)
call.enqueue(object :retrofit2.Callback<EventSearchList>{
override fun onResponse(call: Call<EventSearchList>, response: Response<EventSearchList>) {
Log.d(TAG, "onResponse: on success")
if (response.isSuccessful){
eventSearchList.postValue(response.body()?.event)
Log.d(TAG, "onResponse: ${response.body()}")
}else{
eventSearchList.postValue(null)
}
}
override fun onFailure(call: Call<EventSearchList>, t: Throwable) {
eventSearchList.postValue(null)
Log.d(TAG, "onFailure: call retrofit") }
})
}
另外,不知道你的retrofit2是什么意思。我想你会更好地理解。
我还会建议您将 OKHTTP 拦截器 https://blog.mindorks.com/okhttp-interceptor-making-the-most-of-it 添加到您的改造中,以了解您的网络调用有什么问题
class RetrofitInstance {
companion object{
val BASE_URL ="https://www.thesportsdb.com/api/v1/json/1/"
fun getRetrofitInstance(): Retrofit {
return Retrofit.Builder()
.client(OkHttpClient.Builder().addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)).build())
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
}
对于协程,您可以查看此https://blog.mindorks.com/using-retrofit-with-kotlin-coroutines-in-android并将其与您的工作联系起来