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"
}
}
}
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"
}
}
}