Retrofit 2: App Crashes When Reading JSON Without Internet

23
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:

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()
    }
Answer 1

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.

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.

READ ALSO
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

47
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

35
Finding Javafx node sizes

Finding Javafx node sizes

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

32