这个json API是否正确?

时间:2019-10-05 06:56:20

标签: android kotlin

在api以下是否正确?如果正确,则不会显示“ data” jsonObject。当我使用response.body()时。它只向我显示{“ message”:“ success”}},但其他数据未显示。

我尝试显示很多次,但是总是显示{“ message”:“ success”}。

请帮助我

提前感谢

我的Kotlin代码经过翻新

 service.addressSendToServer(addressModel, token)
            .enqueue(object : Callback<AddressSaveListModel> {
                override fun onResponse(
                    call: Call<AddressSaveListModel>,
                    response: Response<AddressSaveListModel>
                ) {
                    if (response.isSuccessful) {
                        addressSaveListModel.setMessage(response.body()?.getMessage()!!)
                        addressSaveListModel = response.body()!!
                        Log.e("TAG", "response 33: " + Gson().toJson(response.body()))

                        Log.i("SaveDataat",""+response.body()?.getSaveDataAddress()?.getEmail())
   override fun onFailure(call: Call<AddressSaveListModel>, t: Throwable) {
                    Log.d("Repository", "Failed:::" + t.cause + ":" + t.message)
                }
            })





{
"message": "success",
"data": {
    "login_mode": "gm",
    "device": [
        "Android"
    ],
    "_id": "5d96f113eb184f3ff4da1a2f",
    "contact_number": "8755529143",
    "email": "sundarsharma2018@gmail.com",
    "fcm": "8ZJDx26g39bXtxLkbwOXYy0F1HJ2",
    "profile_address": [
        {
            "_id": "5d96f113eb184f3ff4da1a30",
            "address": "Spring House, C28C, LG Flr, Archohm Bldg, C Block, Sector 8, Noida, Uttar Pradesh 201301, India",
            "lat": 28.5942446,
            "lng": 77.3262771,
            "locality": "Noida"
        },
        {
            "_id": "5d971b30eb184f3ff4da1a31",
            "address": "noida",
            "lat": 23,
            "lng": 78,
            "locality": "noida"
        },
        {
            "_id": "5d971c4feb184f3ff4da1a32",
            "address": "noida",
            "lat": 23.565656,
            "lng": 78.566565,
            "locality": "noida"
        },
        {
            "_id": "5d97a5c3346f755cbe2bf41c",
            "address": "noida",
            "lat": 23.565656,
            "lng": 78.566565,
            "locality": "noida"
        },
        {
            "_id": "5d98228b346f755cbe2bf434",
            "address": "noida",
            "lat": 23.565656,
            "lng": 78.566565,
            "locality": "noida"
        }
    ],
    "username": "Your Tutor",
    "__v": 0,
    "otp": 1725,
    "createdAt": "2019-10-05T04:56:43.945Z"
}
}

这是我的模型课

class AddressSaveListModel()  {

@SerializedName("data")
private var dataAddress: DataSaveAddess? = null

@SerializedName("message")
private var message: String? = null



fun getMessage(): String? {
    return message
}

fun setMessage(message: String) {
    this.message = message
}

fun getSaveDataAddress(): DataSaveAddess? {
    return dataAddress
}

fun setSaveDataAddress(dataAddress: DataSaveAddess) {
    this.dataAddress = dataAddress
}
}

class DataSaveAddess {
@SerializedName("login_mode")
private var login_mode: String? = null
@SerializedName("device")
private var device: List<String>? = null
@SerializedName("username")
private var username: String? = null
@SerializedName("email")
private var email: String? = null
@SerializedName("contact_number")
private var contact_number: String? = null
@SerializedName("fcm")
private var fcm: String? = null
@SerializedName("profile_address")
private var profileAddress: List<ProfileAddressSave>? = null
@SerializedName("otp")
private var otp: Int? = null
@SerializedName("_id")
private var id: String? = null



fun getLoginMode(): String? {
    return login_mode
}

fun setLoginMode(login_mode: String) {
    this.login_mode= login_mode
}


fun getDevice(): List<String>? {
    return device
}

fun setDevice(device: List<String>) {
    this.device = device
}

fun getEmail(): String? {
    return email
}

fun setEmail(email: String) {
    this.email= email
}

fun getUsername(): String? {
    return username
}

fun setUsername(username: String) {
    this.username = username
}



fun getContactNumber(): String? {
    return contact_number
}

fun setContactNumber(contact_number: String) {
    this.contact_number = contact_number
}


fun getFcm(): String? {
    return fcm
}

fun setFcm(fcm: String) {
    this.fcm = fcm
}


fun getProfileAddress(): List<ProfileAddressSave>?{
    return profileAddress;
}


fun setProfileAddress(profileAddress: List<ProfileAddressSave>){
    this.profileAddress= profileAddress;
}


fun getOtp(): Int? {
    return otp
}

fun setOtp(otp: Int) {
    this.otp= otp
}


fun getId(): String? {
    return id
}

fun setId(id: String) {
    this.id = id
}


}

class ProfileAddressSave() {


@SerializedName("address")
private var address: String? = null
@SerializedName("locality")
private var locality: String? = null
@SerializedName("lat")
private var lat: Double? = null
@SerializedName("lng")
private var lng: Double? = null
@SerializedName("_id")
private var addressperlistId: String? = null




fun getAddress(): String? {
    return address
}

fun setAddress(address: String) {
    this.address= address
}



fun getLocality(): String? {
    return locality
}

fun setLocality(locality: String) {
    this.locality= locality
}



fun getLong(): Double? {
    return lng
}

fun setLong(lng: Double) {
    this.lng= lng
}


fun getLat(): Double? {
    return lat
}

fun setLat(lat: Double) {
    this.lat= lat
}


fun getId(): String? {
    return addressperlistId
}

fun setId(addressperlistId: String) {
    this.addressperlistId= addressperlistId
}

override fun describeContents(): Int {
    return 0
}

}

输出:

2019-10-05 12:16:21.185 13296-13296/now.bike E/TAG: response 33:                      {"message":"success"}
2019-10-05 12:16:21.185 13296-13296/now.bike I/SaveDataat: null
2019-10-05 12:16:21.194 13296-13296/now.bike I/SaveDataat: success
2019-10-05 12:16:21.195 13296-13296/now.bike W/System.err:    org.json.JSONException: No value for data
2019-10-05 12:16:21.195 13296-13296/now.bike W/System.err:     at org.json.JSONObject.get(JSONObject.java:392)
2019-10-05 12:16:21.195 13296-13296/now.bike W/System.err:     at org.json.JSONObject.getJSONObject(JSONObject.java:612)
2019-10-05 12:16:21.195 13296-13296/now.bike W/System.err:     at now.bike.repository.AddressRepository$insertPostAddresstoserver$1.onResponse(AddressRepository.kt:196)
2019-10-05 12:16:21.195 13296-13296/now.bike W/System.err:     at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
2019-10-05 12:16:21.195 13296-13296/now.bike W/System.err:     at android.os.Handler.handleCallback(Handler.java:873)
2019-10-05 12:16:21.195 13296-13296/now.bike W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:99)
2019-10-05 12:16:21.195 13296-13296/now.bike W/System.err:     at android.os.Looper.loop(Looper.java:201)

2 个答案:

答案 0 :(得分:0)

请尝试如下

service.addressSendToServer(addressModel, token)
            .enqueue(object : Callback<AddressSaveListModel> {
                override fun onResponse(
                        call: Call<AddressSaveListModel>,
                        response: Response<AddressSaveListModel>
                ) {
                    if (response.isSuccessful) {

                        val response_body =response.body()

                        Log.e("TAG", "response 33: " +Gson().toJson(response_body))
                        Log.e("TAG", "response data: " +Gson().toJson(response_body.getSaveDataAddress()))
                        Log.e("TAG", "response message: " +Gson().toJson(response_body .getMessage()))


                        override fun onFailure(call: Call<AddressSaveListModel>, t: Throwable) {
                            Log.d("Repository", "Failed:::" + t.cause + ":" + t.message)
                        }
                    })

答案 1 :(得分:0)

使用这些模型类:


data class AddressSaveListModel (

    @SerializedName("message") val message : String,
    @SerializedName("data") val data : Data
)
data class Data (

    @SerializedName("login_mode") val login_mode : String,
    @SerializedName("device") val device : List<String>,
    @SerializedName("_id") val _id : String,
    @SerializedName("contact_number") val contact_number : Int,
    @SerializedName("email") val email : String,
    @SerializedName("fcm") val fcm : String,
    @SerializedName("profile_address") val profile_address : List<Profile_address>,
    @SerializedName("username") val username : String,
    @SerializedName("__v") val __v : Int,
    @SerializedName("otp") val otp : Int,
    @SerializedName("createdAt") val createdAt : String
)
data class Profile_address (

    @SerializedName("_id") val _id : String,
    @SerializedName("address") val address : String,
    @SerializedName("lat") val lat : Double,
    @SerializedName("lng") val lng : Double,
    @SerializedName("locality") val locality : String
)

然后重试。如果您使用的是kotlin而不是getter setter函数,则最好使用kotlin数据类。并且请检查实际的json“数据”是否不为空。