Home > Mobile >  Google Books API GET Request returns blank screen
Google Books API GET Request returns blank screen

Time:03-08

Good day, All

So I am learning/practicing Network calls. I came across a video by Paul Hudson where he makes a call to the Itunes API using the same code I am trying to use here. However, I am trying to make a call to the Google Books API. My call (code below) is not working, it (returns a blank screen) I am not sure why. I am of course using variables from the URL I am trying to call/make a request from.

import SwiftUI

struct Response: Codable {
    var results: [Result]
}

struct Result: Codable {
    var id: Int
    var etag: String
}

struct ContentView: View {
    @State private var results = [Result]()
    
    var body: some View {
        List(results, id: \.id) { item in
            VStack(alignment: .leading) {
                Text(item.etag)
            }
        }
        .task {
            await loadData()
            // ????
        }
    }
    
    func loadData() async {
        guard let url = URL(string: "https://www.googleapis.com/books/v1/volumes?q=flowers inauthor:keyes") else {
            print("Invalid URL")
            return
        }
        do {
            let (data, _) = try await URLSession.shared.data(from: url)
            if let decodedResponse = try? JSONDecoder().decode(Response.self, from: data) {
                results = decodedResponse.results
            }
        } catch {
            print("Invalid data")
        }
    }
}

As previously mentioned, this code was tested using the Itunes API and it worked flawlessly. I am not sure what is causing the issue or what can fix it. I will keep searching and practicing on my end.

Thank you!

CodePudding user response:

the reason your are getting a blank screen, is because your Response and Result struct do not match the json data you get from the api. Look carefully at the json data and you will see the difference. Try something like this:

struct Response: Codable {
    let items: [Result]  // <--- here
}

struct Result: Codable, Identifiable {  // <--- here
    var id: String      // <--- here
    var etag: String
}

struct ContentView: View {
    @State private var results = [Result]()
    
    var body: some View {
        List(results) { item in  // <--- here
            VStack(alignment: .leading) {
                Text(item.etag)
            }
        }
        .task {
            await loadData()
        }
    }
    
    func loadData() async {
         guard let url = URL(string: "https://www.googleapis.com/books/v1/volumes?q=flowers inauthor:keyes") else {
             print("Invalid URL")
             return
         }
         do {
             let (data, _) = try await URLSession.shared.data(from: url)
             if let decodedResponse = try? JSONDecoder().decode(Response.self, from: data) {
                 results = decodedResponse.items  // <--- here
             }
         } catch {
             print("Invalid data")
         }
     }
    
}
  • Related