I was testing my app to handle an event of a SocketTimeoutException which should be handled by the OnFailure method in Call.Enqueue
NOTE: I reduce the TimeOutEXCEPTION timer to 10ms.
Repository
override fun onFailure(call: Call<NewsDataFromJson>, t: Throwable) {
MainActivity.apiRequestError = true
Log.d("err_msg", "MainActivty.apiRequestError value: ${MainActivity.apiRequestError}")
Log.d("err_msg", "msg: ${t.message}")
MainActivity.errorMessage = t.localizedMessage as String
}
When the code is ran, the log produces the correct responses, I reassigned the apiRequestError variable Companion object to true so I can handle the response in the MainActivity
So the way I implemented to deal with OnsocketTimeoutException is to reassign the variables apiRequestError and errorMessage which is located in the MainActivity Companion Object.
2023-05-15 17:58:33.292 3638-3638/com.example.newstest D/err_msg: MainActivty.apiRequestError value: true
2023-05-15 17:09:34.267 3372-3372/com.example.newstest D/err_msg: msg: failed to connect to newsapi.org/104.26.13.149 (port 443) from /10.0.2.16 (port 44172) after 10ms
This is how I call the requests in the MainActivity Oncreate method, upon running the app, the Log below would display False even though it is supposed to be True.
lifecycleScope.launch(Dispatchers.Main) {
delay(1000)
Log.d("MainActivity","Answer: $apiRequestError" )
requestNews(GENERAL, generalNews, "us")
requestNews(TECHNOLOGY, TechNews, "us")
requestNews(HEALTH, healthNews, "us")
requestNews(SPORTS, SportsNews, "us")
requestNews(ENTERTAINMENT, EntertainmentNews, "us")
requestNews(SCIENCE, ScienceNews, "us")
requestNews(BUSINESS, BusinessNews, "us")
}
The Log statement is
2023-05-15 17:09:34.062 3372-3372/com.example.newstest D/MainActivity: Answer: false
This is the way I handle each request in the MainActivity.
suspend private fun requestNews(newsCategory: String, newsData: MutableList<Article>,country:String) {
viewModel.getNews(category = newsCategory, Country = country)?.observe(this) {
newsData.addAll(it)
totalRequestCount += 1
lifecycleScope.launch(Dispatchers.Main) {
if (ScienceNews.isNotEmpty() && BusinessNews.isNotEmpty() && EntertainmentNews.isNotEmpty() && generalNews.isNotEmpty() && healthNews.isNotEmpty() && SportsNews.isNotEmpty() && TechNews.isNotEmpty()) {
Log.d("MainActivity","Answer $OnSocketTimeOut 1" )
if (apiRequestError == false) {
Log.d("MainActivity","Answer $OnSocketTimeOut 2" )
ProgresBar.visibility = View.GONE
FragmentContainer.visibility = View.VISIBLE
} else if (apiRequestError == true) {
Log.d("MainActivity","Answer $OnSocketTimeOut 3" )
ProgresBar.visibility = View.GONE
FragmentContainer.visibility = View.GONE
val showError: TextView = findViewById(R.id.display_error)
showError.text = errorMessage
showError.visibility = View.VISIBLE
}
} else if (apiRequestError == true) {
Log.d("MainActivity","Answer $OnSocketTimeOut 4" )
ProgresBar.visibility = View.GONE
FragmentContainer.visibility = View.GONE
val showError: TextView = findViewById(R.id.display_error)
showError.text = errorMessage
showError.visibility = View.VISIBLE
}
}
}
}
Since the Companion object variable apiRequestError is giving the wrong value, my app is in a perpetual state of loading whenever a SocketTimeoutException occurs.
Is there a way to convey the correct value from the Repository to the MainActivity?
