Simple way to load remote json file into Android app ?(video view)

328 views Asked by At

I would like to run this the same way as I do it's Swift counterpart. In the Swift version I'm loading the json into a parser that loads the list in id (highest to lowest) and displays the thumbnail image and title (provided by the json) and when clicked it shows the video of the attached ID.

I'm currently seeing nothing similar to the SwiftUI (list view in particular) setup, if you can provide any resources or examples to as such I would appreciate it.

Here is one of the SwiftUI files

import SwiftUI
import Combine

struct VideoList: View {
@Environment(\.presentationMode) private var presentationMode
@ObservedObject private(set) var viewModel: ViewModel
@State private var isRefreshing = false

var btnBack : some View { Button(action: {
    self.presentationMode.wrappedValue.dismiss()
    }) {
        HStack {
        Image("Home") // set image here
            .aspectRatio(contentMode: .fit)
            .foregroundColor(.white)
        }
    }
}

var body: some View {
    NavigationView {
        List(viewModel.videos.sorted { $0.id > $1.id}, id: \.id) { video in
            NavigationLink(
            destination: VideoDetails(viewModel: VideoDetails.ViewModel(video: video))) {
                VideoRow(video: video)
                
            }
        }
        .onPullToRefresh(isRefreshing: $isRefreshing, perform: {
            self.viewModel.fetchVideos()
        })
        .onReceive(viewModel.$videos, perform: { _ in
            self.isRefreshing = false
        })
    }
    .onAppear(perform: viewModel.fetchVideos)
    .navigationViewStyle(StackNavigationViewStyle())
    .navigationBarBackButtonHidden(true)
            .navigationBarItems(leading: btnBack)

}
}

Here is a clip from the json it is loading

{
"videos": [
    {
        "id": 97,
        "name": "name",
        "thumbnail": "https://videodelivery.net//thumbnails/thumbnail.jpg",
        "description": "October 11th, 2020",
        "video_link": "https://videodelivery.net//manifest/video.m3u8"
    },
    {
        "id": 96,
        "name": "name",
        "thumbnail": "https://videodelivery.net//thumbnails/thumbnail.jpg",
        "description": "October 11th, 2020",
        "video_link": "https://videodelivery.net//manifest/video.m3u8"
    },
1

There are 1 answers

5
Rishabh Ritweek On

https://riptutorial.com/android/example/29241/try-offline-disk-cache-first--then-go-online-and-fetch-the-image

This is a fine example of caching thumbnails , follow this , only change would be :You write picasso.load in your adapter's getView method.Just set the imageview sing Picasso.Now, you can pass the video url when list view when tapped by using an intent inside onItemCLickListener and then in the destination activity, you can have a medi player where you can use that url to just set it on the medi player and play the video.

This solution will work for lists as well.

Also since picasso use memory caching by default, we have to use OKHTTP disk Cache as well because othewise Picaso would just check thumbnails in memory cache and would fail.