Retrofit 2: App Crashes When Reading JSON Without Internet

February 22, 2021, at 09:30 AM

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:


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(
        viewModel.apply {
            manufacturer.observe(requireActivity(), {loadRecyclerView(it)})

View Model

class ManuFragmentVM : ViewModel() {
    val manufacturer = MutableLiveData<List<Manufacturers>>()
    fun setup() {
        viewModelScope.launch(Dispatchers.Default) {


interface ManufacturerApi {
    suspend fun fetchAllManufacturers(): List<Manufacturers>


class CarsRepository {
    private fun manufacturerRetrofit(): ManufacturerApi {
        return Retrofit.Builder()
    suspend fun getAllManufacturers(): List<Manufacturers> {
        return manufacturerRetrofit().fetchAllManufacturers()
Answer 1

It looks like you only need to wrap your call in a try-catch:

fun setup() {
    viewModelScope.launch(Dispatchers.IO) {
        try {
        } 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.

Answer 2

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.

php mysql search for text

php mysql search for text

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

Fixing Unbound local error: local variable reference before assignment

Fixing Unbound local error: local variable reference before assignment

I am getting an error saying "UnboundLocalError: local variable 'result_numerator' referenced before assignment" and not sure whyAny kind of help will be appreciated

Finding Javafx node sizes

Finding Javafx node sizes

I read somewhere that thegetWidth() function only works when the node itself is properly initiated