Home > Software design >  Retrieving Graph Data using JSON and API data for Android App
Retrieving Graph Data using JSON and API data for Android App

Time:02-03

I am trying to create an Android mobile app primarily in Kotlin to track Covid-19 data. I am using retrofit to retrieve the API data. However when running the application to test my data come up blank. For some reason the API data is not being retrieved. The site I'm using is CovidActNow.org which requires an API key.

I call the base URL in my MainActivity file with:

private const val BASE_URL = "https://api.covidactnow.org/v2/"

and then I've also created a Kotlin file called CovidData which contains:

package com.example.covid19tracker

import com.google.gson.annotations.SerializedName
import java.util.*


data class CovidData(
    @SerializedName("date") val date: Date,
    @SerializedName("positiveTests") val positiveTests: Int,
    @SerializedName("negativeTests") val negativeTests: Int,
    @SerializedName("newDeaths") val newDeaths: Int,
    @SerializedName("state") val state: String

)

and Services which should be getting the data with:

package com.example.covid19tracker

import retrofit2.Call
import retrofit2.http.GET

interface Services {
    @GET("country/US.json?apiKey={myAPIkeywithoutbrackets}")
    fun getNationalData(): Call<List<CovidData>>

    @GET("states.json?apiKey={myAPIkeywithoutbrackets}")
    fun getStatesData(): Call<List<CovidData>>
}

I'm not sure if I'm just calling the API key incorrectly or if I have a typo, but I would appreciat any feedback or help!!

CodePudding user response:

what is the response code and body you are receiving after making the request?

CodePudding user response:

Please make sure that the:- 1:- Base URL is pointing to the correct API 2:- country/US.json?apiKey={myAPIkeywithoutbrackets} is returning the list of the CovidData 3:- Change your object into this:-

package com.example.covid19tracker

import com.google.gson.annotations.SerializedName
import java.util.*

@Parcelize
data class CovidData(
    @SerializedName("date") val date: Date,
    @SerializedName("positiveTests") val positiveTests: Int,
    @SerializedName("negativeTests") val negativeTests: Int,
    @SerializedName("newDeaths") val newDeaths: Int,
    @SerializedName("state") val state: String
): Parcelable
  •  Tags:  
  • Related