Home > Software design >  Retrofit2 get response JSONobject
Retrofit2 get response JSONobject

Time:04-29

I'm have little bit problem in my project i want get a JSON from this API: https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&limit=10.

API

import com.earthquakereport.data.model.EarthquakeReportData
import retrofit2.Response
import retrofit2.http.GET

interface ApiService {
    @GET("fdsnws/event/1/query?format=geojson&limit=10")
    suspend fun getEarthQuakeReport(): Response<EarthquakeReportData>
}

object RetrofitInstance {
    private val retrofit = Retrofit.Builder()
        .baseUrl("https://earthquake.usgs.gov/")
        .addConverterFactory(GsonConverterFactory.create())
        .build()

    val api: ApiService = retrofit.create(ApiService::class.java)
}

CloudDataSource

import android.util.Log
import com.earthquakereport.cloud.api.RetrofitInstance
import com.earthquakereport.data.model.EarthquakeReportData

interface CloudDataSource {

    suspend fun getEarthquakeReportData(): EarthquakeReportData?

    class Base : CloudDataSource {

        override suspend fun getEarthquakeReportData(): EarthquakeReportData? {
            Log.d("blushful2", RetrofitInstance.api.getEarthQuakeReport().code().toString())
            return RetrofitInstance.api.getEarthQuakeReport().body()
        }
    }
}

In other class i get Log .body(), but my list is empty.

Models

data class EarthquakeData(
    val alert: Any,
    val cdi: Any,
    val code: String,
    val detail: String,
    val dmin: Double,
    val felt: Any,
    val gap: Double,
    val ids: String,
    val mag: Double,
    val magType: String,
    val mmi: Any,
    val net: String,
    val nst: Int,
    val place: String,
    val rms: Double,
    val sig: Int,
    val sources: String,
    val status: String,
    val time: Long,
    val title: String,
    val tsunami: Int,
    val type: String,
    val types: String,
    val tz: Any,
    val updated: Long,
    val url: String
)
data class EarthquakeReportData(
    val list: ArrayList<EarthquakeData>
)

Log

Image

JSON

enter image description here

Please help me get all the "properties" from JSON. Thx.

CodePudding user response:

you need tell response this is a list

interface ApiService {
    @GET("fdsnws/event/1/query?format=geojson&limit=10")
    suspend fun getEarthQuakeReport(): Response<List<EarthquakeReportData>>
}

CodePudding user response:

use https://www.jsonschema2pojo.org/ for get classes you need

package com.example;

import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("jsonschema2pojo")
public class EarthquakeReportData{

@SerializedName("type")
@Expose
public String type;
@SerializedName("metadata")
@Expose
public Metadata metadata;
@SerializedName("features")
@Expose
public List<Feature> features = null;
@SerializedName("bbox")
@Expose
public List<Float> bbox = null;

}
-----------------------------------com.example.Feature.java-----------------------------------

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("jsonschema2pojo")
public class Feature {

@SerializedName("type")
@Expose
public String type;
@SerializedName("properties")
@Expose
public Properties properties;
@SerializedName("geometry")
@Expose
public Geometry geometry;
@SerializedName("id")
@Expose
public String id;

}
-----------------------------------com.example.Geometry.java-----------------------------------

package com.example;

import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("jsonschema2pojo")
public class Geometry {

@SerializedName("type")
@Expose
public String type;
@SerializedName("coordinates")
@Expose
public List<Float> coordinates = null;

}
-----------------------------------com.example.Metadata.java-----------------------------------

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("jsonschema2pojo")
public class Metadata {

@SerializedName("generated")
@Expose
public Long generated;
@SerializedName("url")
@Expose
public String url;
@SerializedName("title")
@Expose
public String title;
@SerializedName("status")
@Expose
public Integer status;
@SerializedName("api")
@Expose
public String api;
@SerializedName("limit")
@Expose
public Integer limit;
@SerializedName("offset")
@Expose
public Integer offset;
@SerializedName("count")
@Expose
public Integer count;

}
-----------------------------------com.example.Properties.java-----------------------------------

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("jsonschema2pojo")
public class Properties {

@SerializedName("mag")
@Expose
public Float mag;
@SerializedName("place")
@Expose
public String place;
@SerializedName("time")
@Expose
public Long time;
@SerializedName("updated")
@Expose
public Long updated;
@SerializedName("tz")
@Expose
public Object tz;
@SerializedName("url")
@Expose
public String url;
@SerializedName("detail")
@Expose
public String detail;
@SerializedName("felt")
@Expose
public Object felt;
@SerializedName("cdi")
@Expose
public Object cdi;
@SerializedName("mmi")
@Expose
public Object mmi;
@SerializedName("alert")
@Expose
public Object alert;
@SerializedName("status")
@Expose
public String status;
@SerializedName("tsunami")
@Expose
public Integer tsunami;
@SerializedName("sig")
@Expose
public Integer sig;
@SerializedName("net")
@Expose
public String net;
@SerializedName("code")
@Expose
public String code;
@SerializedName("ids")
@Expose
public String ids;
@SerializedName("sources")
@Expose
public String sources;
@SerializedName("types")
@Expose
public String types;
@SerializedName("nst")
@Expose
public Integer nst;
@SerializedName("dmin")
@Expose
public Object dmin;
@SerializedName("rms")
@Expose
public Float rms;
@SerializedName("gap")
@Expose
public Integer gap;
@SerializedName("magType")
@Expose
public String magType;
@SerializedName("type")
@Expose
public String type;
@SerializedName("title")
@Expose
public String title;

}
  • Related