androidkotlinretrofit2

Retrofit 2: App Crashes When Reading JSON Without Internet


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()

    }

Solution

  • 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
            }
        }
    }
    

    The rest of your code looks really good.