NASA Space API Android App | Youtube Video & Github

ibrahimcanerdogan
4 min readJun 3, 2023

--

Photo by NASA on Unsplash

Hello in this article, we will develop an application using the NASA Space API.

GOOGLE PLAY

What is the Nasa Space API?

NASA is opening up a world of possibilities with the NASA Space API — an innovative platform that grants access to a vast collection of space-related data and resources. This powerful API allows developers, researchers, and space enthusiasts to tap into NASA’s extensive repositories, unlocking new insights and fueling advancements in space science. With seamless access to satellite imagery, astronomical observations, and mission details, the NASA Space API is revolutionizing the way we explore the cosmos.

Nasa Space App Introduction

The application consists of 3 menus: APOD (Astronomy Picture of the Day), Mars Rover Photos and EPIC (Earth Polychromatic Imaging Camera).

The application was developed using the MVVM architecture. The most important libraries of the Android world such as Retrofit, Room database, Dagger-Hilt and Glide are used. Fragments brought on the navigation structure have been developed in accordance with the lifecycle structure.

APOD Screen

Data Class

Create variables for API data.

@Entity(tableName = "apod")
data class Astronomy(
@PrimaryKey(autoGenerate = true)
val id : Int,
@SerializedName("title")
val astronomyTitle : String,
@SerializedName("date")
val astronomyDate : String,
@SerializedName("explanation")
val astronomyExplanation : String,
@SerializedName("url")
val astronomyImage : String?
)

API Service

Using retrofit GET method for NASA API data.

@GET("planetary/apod?count=20")
suspend fun getAstronomersFromAPI(@Query("api_key") apiKey : String) : Response<List<Astronomy>>

Room Dao

Rom integration for offline use of the application and not making continuous API requests.

@Dao
interface AstronomyDao {

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun saveAstronomyData(astronomyList : List<Astronomy>)

@Query("DELETE FROM apod")
suspend fun deleteAllAstronomyData()

@Query("SELECT * FROM apod")
suspend fun getAllAstronomyData() : List<Astronomy>
}

Repository

We transfer all the information we collect in the background of the Repository file to the ViewModel.

interface AstronomyRepository {
suspend fun getAstronomyDataFromRepository() : List<Astronomy>?
suspend fun updateAstronomyDataFromRepository() : List<Astronomy>?
}

ViewModel

We make it ready to be used on the fragment.

class AstronomyViewModel(
private val getAstronomyUseCase: GetAstronomyUseCase,
private val updateAstronomyUseCase: UpdateAstronomyUseCase
) : ViewModel() {

fun getAstronomyData() = liveData {
val astronomyList = getAstronomyUseCase.execute()
emit(astronomyList)
}

fun updateAstronomyData() = liveData {
val astronomyList = updateAstronomyUseCase.execute()
emit(astronomyList)
}
}

Mars Rover Screen

Data Class

Create variables for API data.

data class MarsRover(
@PrimaryKey(autoGenerate = true)
val dataId: Int,
@SerializedName("photos")
val dataMarsRover: List<MarsRoverDetail>
)

API Service

Using retrofit GET method for NASA API data.

    @GET("mars-photos/api/v1/rovers/curiosity/photos?sol=1000")
suspend fun getMarsRoverDataFromAPI(
@Query("page") page : Int,
@Query("api_key") apiKey: String
) : Response<MarsRover>

Room Dao

Rom integration for offline use of the application and not making continuous API requests.

@Dao
interface MarsRoverDao {

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun saveMarsRoverData(astronomyList : List<MarsRoverDetail>)

@Query("DELETE FROM marsroverdetail")
suspend fun deleteAllMarsRoverData()

@Query("SELECT * FROM marsroverdetail")
suspend fun getAllMarsRoverData() : List<MarsRoverDetail>

}

Repository

We transfer all the information we collect in the background of the Repository file to the ViewModel.

interface MarsRoverRepository {

suspend fun getMarsRoverDataFromRepository(page : Int) : List<MarsRoverDetail>?

suspend fun updateMarsRoverDataFromRepository(page : Int) : List<MarsRoverDetail>?

}

ViewModel

We make it ready to be used on the fragment.

class MarsRoverViewModel(
private val getMarsRoverUseCase : GetMarsRoverUseCase,
private val updateMarsRoverUseCase: UpdateMarsRoverUseCase
) : ViewModel() {

fun getMarsRoverData(page : Int) = liveData {
val marsRoveData = getMarsRoverUseCase.execute(page)
emit(marsRoveData)
}

fun updateMarsRoverData(page : Int) = liveData {
val marsRoveData = updateMarsRoverUseCase.execute(page)
emit(marsRoveData)
}
}

EPIC (Earth Polychromatic Imaging Camera) Screen

Data Class

Create variables for API data.

class Earth : ArrayList<EarthDetail>()
@Entity(tableName = "earthdetail")
data class EarthDetail(
@PrimaryKey
@SerializedName("identifier")
val earthId : Long,
@SerializedName("caption")
val earthCaption: String,
@SerializedName("image")
val earthImageCode: String,
@SerializedName("centroid_coordinates")
val earthCentroidCoordinates: EarthCentroidCoordinates,
@SerializedName("dscovr_j2000_position")
val earthDscovrJ2000Position: EarthDscovrJ2000PositionX,
@SerializedName("attitude_quaternions")
val earthAttitudeQuaternions: EarthAttitudeQuaternions,
@SerializedName("lunar_j2000_position")
val earthLunarJ2000Position: EarthLunarJ2000PositionX,
@SerializedName("sun_j2000_position")
val earthSunJ2000Position: EarthSunJ2000PositionX,
@SerializedName("date")
val earthDate: String
)

API Service

Using retrofit GET method for NASA API data.

    @GET("https://api.nasa.gov/EPIC/api/natural/date/{today_date}")
suspend fun getEarthFromAPI(
@Path("today_date") todayDate: String,
@Query("api_key") apiKey: String
) : Response<Earth>

Room Dao

Rom integration for offline use of the application and not making continuous API requests.

@Dao
interface EarthDao {

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun saveEarthData(astronomyList : List<EarthDetail>)

@Query("DELETE FROM earthdetail")
suspend fun deleteAllEarthData()

@Query("SELECT * FROM earthdetail")
suspend fun getAllEarthData() : List<EarthDetail>
}

Repository

We transfer all the information we collect in the background of the Repository file to the ViewModel.

interface EarthRepository {

suspend fun getEarthDataFromRepository(todayDate : String) : List<EarthDetail>?

}

ViewModel

We make it ready to be used on the fragment.

class EarthViewModel(
private val getEarthUseCase: GetEarthUseCase
) : ViewModel() {

fun getEarthData(todayDate : String) = liveData {
val earthList = getEarthUseCase.execute(todayDate)
emit(earthList)
}
}

I hope it was useful. You can follow me on my social media accounts.

LINKEDIN

UDEMY

GITHUB

--

--

ibrahimcanerdogan
ibrahimcanerdogan

Written by ibrahimcanerdogan

Hi, My name is Ibrahim, I am developing ebebek android app within Ebebek. I publish various articles in the field of programming and self-improvement.

No responses yet