• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

don't know how to access data using retrofit

Gioele

Lurker
I'm writing an application that have to get data from this site: https://api.spacex.land/graphql/.

I'm using retrofit for the first time and I don't have idea how I have to set the API service to get, for example, the launchLatest from this site.

This is my model:

data class LaunchModel(
var details: String,
var id: String,
var isTentative: Boolean,
var launchDateLocal: Date,
var launchDateUnix: Date,
var launchDateUtc: Date,
var launchSite: LaunchSiteModel,
var launchSuccess: Boolean,
var launchYear: String,
var links: LaunchLinksModel,
var missionId: String,
var missionName: String,
var rocket: LaunchRocketModel,
var staticFireDateUnix: Date,
var staticFireDateUtc: Date,
var telemetry: LaunchTelemetryModel,
var tentativeMaxPrecision: String,
var upComing: Boolean,
var ships : ShipModel,
)

this is the RetroIstance, a class that instance the settings of retrofit:

class RetroInstance {
companion object{
private const val baseUrl = "https://api.spacex.land/"

fun getRetroInstance() : Retrofit {

val client = OkHttpClient.Builder()
return Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.client(client.build())
.build()
}
}
}

this is my main activity:

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val retroInstance = RetroInstance.getRetroInstance().create(RetroService::class.java)
val retrofitData = retroInstance.getLastLaunch()
retrofitData.enqueue(object: Callback<LaunchModel> {
override fun onFailure(call: Call<LaunchModel>, t: Throwable) {
Toast.makeText(this@MainActivity, "call fail", Toast.LENGTH_LONG).show()
}

override fun onResponse(
call: Call<LaunchModel>,
response: Response<LaunchModel>
) {
val responseBody = response.body()

if(responseBody != null){
Toast.makeText(this@MainActivity, "${responseBody.id} + ${responseBody.details}", Toast.LENGTH_LONG).show()
}else {
Toast.makeText(this@MainActivity, "null calling! ${responseBody}", Toast.LENGTH_LONG).show()
}
}
})
}
}

and this is the interface where I try to set the method that get the launchLatest from the site:

interface RetroService {
@GET("graphql/")
fun getLastLaunch () : Call<LaunchModel>
}

I can't understand how to set this method to retrive the date from launchLatest. Can someone help me please? thanks

This is the output when a make the request from the browser:

{
"data": {
"launchLatest": {
"id": "109"
}
}
}
 
To retrieve the launchLatest data from the SpaceX GraphQL API using Retrofit, you can update your Retrofit service interface as follows:

Code:
interface RetroService {
  @POST("graphql/")
  @Headers("Content-Type: application/json")
  fun getLastLaunch(): Call<LaunchModel>

  data class LaunchModel(val data: Data)

  data class Data(val launchLatest: LaunchLatest)

  data class LaunchLatest(
    val id: String,
    val details: String?,
    val isTentative: Boolean?,
    val launchDateLocal: String?,
    val launchDateUnix: Long?,
    val launchDateUtc: String?,
    val launchSite: LaunchSite?,
    val launchSuccess: Boolean?,
    val launchYear: String?,
    val links: Links?,
    val missionId: List<String>?,
    val missionName: String?,
    val rocket: Rocket?,
    val staticFireDateUnix: Long?,
    val staticFireDateUtc: String?,
    val telemetry: Telemetry?,
    val tentativeMaxPrecision: String?,
    val upcoming: Boolean?,
    val ships: List<Ship>?
  )

  data class LaunchSite(val siteId: String, val siteNameLong: String)

  data class Links(val flickrImages: List<String>, val missionPatch: String, val missionPatchSmall: String, val presskit: String, val redditCampaign: String, val redditLaunch: String, val redditRecovery: String, val redditMedia: String, val videoLink: String)

  data class Rocket(val rocketId: String, val rocketName: String, val rocketType: String)

  data class Telemetry(val flightClub: String)

  data class Ship(val shipId: String, val shipName: String, val shipModel: String)
}

To make the request, you can use the following code:

Code:
val retroInstance = RetroInstance.getRetroInstance().create(RetroService::class.java)
val requestBody = "{\"query\":\"{launchLatest{id, details, isTentative, launchDateLocal, launchDateUnix, launchDateUtc, launchSite{siteId, siteNameLong}, launchSuccess, launchYear, links{flickrImages, missionPatch, missionPatchSmall, presskit, redditCampaign, redditLaunch, redditRecovery, redditMedia, videoLink}, missionId, missionName, rocket{rocketId, rocketName, rocketType}, staticFireDateUnix, staticFireDateUtc, telemetry{flightClub}, tentativeMaxPrecision, upcoming, ships{shipId, shipName, shipModel}}}\",\"variables\":null}"
val retrofitData = retroInstance.getLastLaunch(requestBody)
retrofitData.enqueue(object: Callback<RetroService.LaunchModel> {
  override fun onFailure(call: Call<RetroService.LaunchModel>, t: Throwable) {
    Toast.makeText(this@MainActivity, "call fail", Toast.LENGTH_LONG).show()
  }

  override fun onResponse(
    call: Call<RetroService.LaunchModel>,
    response: Response<RetroService.LaunchModel>
  ) {
    val responseBody = response.body()

    if(responseBody != null){
      val
 
Back
Top Bottom