I am trying to stop my app from crashing when trying to read json information using Gson and Retrofit2 when user has no internet connection. Any Ideas?
My code:
Fragment
class ManuFragment : Fragment() {
private lateinit var viewModel: ManuFragmentVM
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
_binding = FragmentManuBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel = ViewModelProvider(this).get(ManuFragmentVM::class.java)
viewModel.apply {
manufacturer.observe(requireActivity(), {loadRecyclerView(it)})
setup()
}
}
View Model
class ManuFragmentVM : ViewModel() {
val manufacturer = MutableLiveData<List<Manufacturers>>()
fun setup() {
viewModelScope.launch(Dispatchers.Default) {
manufacturer.postValue(CarsRepository().getAllManufacturers())
}
}
Interface
interface ManufacturerApi {
@GET("url.json")
suspend fun fetchAllManufacturers(): List<Manufacturers>
}
Repository
class CarsRepository {
private fun manufacturerRetrofit(): ManufacturerApi {
return Retrofit.Builder()
.baseUrl("https://website.com/")
.addConverterFactory(GsonConverterFactory.create(GsonBuilder().create()))
.build()
.create(ManufacturerApi::class.java)
}
suspend fun getAllManufacturers(): List<Manufacturers> {
return manufacturerRetrofit().fetchAllManufacturers()
}
It looks like you only need to wrap your call in a try-catch:
fun setup() {
viewModelScope.launch(Dispatchers.IO) {
try {
manufacturer.postValue(CarsRepository().getAllManufacturers())
} catch (ce: CancellationException) {
throw ce // Needed for coroutine scope cancellation
} catch (e: Exception) {
// display error
}
}
}
Also, you should run API calls explictly with the IO dispatcher. The rest of your code looks really good.
You can wrap manufacturer.postValue(CarsRepository().getAllManufacturers())
with try catch
block. If you want to tell user about that error, create another LiveData object, e.g. val error: MutableLiveData<String>
and post error text from catch
block.
How to kill a query running by pd.read_sql and connected by sqlalchemy (or mysql.connector)
Radio and checkbox cannot be checked when inside BootStrap Modal
I am writing a blog, and i have to post things in series, and i need to link them togetherI want to input the next post id to the previous post
I am getting an error saying "UnboundLocalError: local variable 'result_numerator' referenced before assignment" and not sure whyAny kind of help will be appreciated
I read somewhere that thegetWidth() function only works when the node itself is properly initiated