【问题标题】:Json object response in kotlinkotlin 中的 Json 对象响应
【发布时间】:2020-10-06 12:19:06
【问题描述】:

我在 kotlin 中获得响应时遇到问题。我的 json 响应是:

{
   "message":"Success",
   "status":false,
   "data":[
      {
         "id":"1",
         "username":"doctor",
         "phone":null,
         "speciality":"General Physician",
         "name":"Doctor",
         "firstname":null,
         "lastname":null,
         "gender":"",
         "age":null,
         "dobirth":null,
         "email":"doctor@live.com",
         "country":"Pakistan",
         "state":"Punjab",
         "city":"Lahore",
         "address":null,
         "affiliation":"",
         "degree":"MBBS",
         "bio":null,
         "password":"$2y$10$KF1zBxe07nPBW.0hFWiFfOjIur4cYYfP.LlQlujjcHq4WmQMLGWLK",
         "remember_token":"UlekRgPJqWPx9AczdW2D7cyjiWkyU4mDpGYkR2QYovjsDCaVTt7adnQmSJQo",
         "image":"1496739459-ariba.jpg",
         "license_owner":"0",
         "status":"0",
         "switch_role":"1",
         "invitation_code":"",
         "created_at":"2018-10-01 07:55:47",
         "updated_at":"2018-01-26 00:02:50",
         "license_purchase_id":"0",
         "profile_active":"0",
         "pmdc":"",
         "flag":"1"
      },
      {
         "id":"2",
         "username":"khawarshah",
         "phone":null,
         "speciality":"",
         "name":"Syed Khawar",
         "firstname":null,
         "lastname":null,
         "gender":"",
         "age":null,
         "dobirth":null,
         "email":"jjshjasd@tech4lifeenterprises.com",
         "country":"",
         "state":"",
         "city":"",
         "address":null,
         "affiliation":"",
         "degree":"",
         "bio":null,
         "password":"$2y$10$3nG\/43tUdA2QKzinBPvA4.zqQHfxmR8sZ0LICQ3xg6LLr6mFYZq7q",
         "remember_token":"teuzY7HKubHdQg9TXA3zgDJmszrNPm2vBg1226JmDPhk0APZuEafIUpNGKJ4",
         "image":"",
         "license_owner":"0",
         "status":"1",
         "switch_role":"1",
         "invitation_code":"",
         "created_at":"2019-04-19 07:08:10",
         "updated_at":"2019-04-19 11:08:10",
         "license_purchase_id":"0",
         "profile_active":"0",
         "pmdc":"",
         "flag":"1"
      }
  ]
}

ApiClient类和ApiFactory是

data class Users (

    @Expose
    @SerializedName("message")
    val message: String,
    @Expose
    @SerializedName("status")
    val status: String,
    @Expose
    @SerializedName("id")
    val id: String,
    @Expose
    @SerializedName("username")
    val username: String,
    @Expose
    @SerializedName("speciality")
    val speciality: String,
    @Expose
    @SerializedName("firstname")
    val firstname: String,
    @Expose
    @SerializedName("name")
    val name: String,
    @Expose
    @SerializedName("lastname")
    val lastname: String,
    @Expose
    @SerializedName("gender")
    val gender: String,
    @Expose
    @SerializedName("age")
    val age: String,
    @Expose
    @SerializedName("dobirth")
    val dobirth: String,
    @Expose
    @SerializedName("email")
    val email: String,
    @Expose
    @SerializedName("image")
    val image: String,
    @Expose
    @SerializedName("switch_role")
    val switch_role: String

)

主要片段是

class HomeFragment : Fragment() {

    private lateinit var homeViewModel: HomeViewModel

    lateinit var progerssProgressDialog: ProgressDialog
    var dataList = ArrayList<Users>()
    lateinit var recyclerView: RecyclerView
    lateinit var adapter:ListAdapter


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        homeViewModel =
            ViewModelProviders.of(this).get(HomeViewModel::class.java)
        val root = inflater.inflate(R.layout.fragment_home, container, false)
        val RecyclerView: RecyclerView = root.findViewById(R.id.recyclerView)
        recyclerView = root.findViewById(R.id.recyclerView)
        recyclerView.adapter= ListAdapter(dataList,requireContext())
        recyclerView.layoutManager=LinearLayoutManager(requireContext(),LinearLayoutManager.VERTICAL,false)

        progerssProgressDialog=ProgressDialog(requireContext())
        progerssProgressDialog.setTitle("Loading")
        progerssProgressDialog.setCancelable(false)
        progerssProgressDialog.show()
        getData()

        return root
    }


    private fun getData() {
        val call: Call<List<Users>> = ApiClient.getClient.getPhotos()
        call.enqueue(object : Callback<List<Users>> {

            override fun onResponse(call: Call<List<Users>>?, response: Response<List<Users>>?) {
                progerssProgressDialog.dismiss()
                dataList.addAll(response!!.body()!!)

                recyclerView.adapter?.notifyDataSetChanged()
            }

            override fun onFailure(call: Call<List<Users>>, t: Throwable) {
                progerssProgressDialog.dismiss()
            }
        })
    }
}

但在 recyclerview 中没有得到响应。什么错误?

【问题讨论】:

    标签: android json object kotlin


    【解决方案1】:

    您的 Users 类与您的 json 结构不匹配,因此您将无法解析它。查看您的 json,它更像是:

    data class ResponseMessage(
        val message: String,
        val status: Boolean,
        val data: List<User>
    )
    
    data class User(
        val id: Long,
        val username: String,
        ...
    )
    

    【讨论】:

      【解决方案2】:

      您的数据类和 JSON 结构不匹配。

      在Android Studio中使用JsonToKotlinClass插件从json创建数据类

      【讨论】:

        【解决方案3】:

        问题在于您的数据类“用户”。请查看以下数据类以供参考:

        data class Response(
        
        @field:SerializedName("data")
        val data: List<DataItem?>? = null,
        
        @field:SerializedName("message")
        val message: String? = null,
        
        @field:SerializedName("status")
        val status: Boolean? = null)
        
        data class DataItem(
        
        @field:SerializedName("country")
        val country: String? = null,
        
        @field:SerializedName("firstname")
        val firstname: Any? = null,
        
        @field:SerializedName("invitation_code")
        val invitationCode: String? = null,
        
        @field:SerializedName("flag")
        val flag: String? = null,
        
        @field:SerializedName("gender")
        val gender: String? = null,
        
        @field:SerializedName("city")
        val city: String? = null,
        
        @field:SerializedName("pmdc")
        val pmdc: String? = null,
        
        @field:SerializedName("bio")
        val bio: Any? = null,
        
        @field:SerializedName("created_at")
        val createdAt: String? = null,
        
        @field:SerializedName("switch_role")
        val switchRole: String? = null,
        
        @field:SerializedName("speciality")
        val speciality: String? = null,
        
        @field:SerializedName("password")
        val password: String? = null,
        
        @field:SerializedName("updated_at")
        val updatedAt: String? = null,
        
        @field:SerializedName("affiliation")
        val affiliation: String? = null,
        
        @field:SerializedName("license_owner")
        val licenseOwner: String? = null,
        
        @field:SerializedName("id")
        val id: String? = null,
        
        @field:SerializedName("state")
        val state: String? = null,
        
        @field:SerializedName("remember_token")
        val rememberToken: String? = null,
        
        @field:SerializedName("email")
        val email: String? = null,
        
        @field:SerializedName("image")
        val image: String? = null,
        
        @field:SerializedName("license_purchase_id")
        val licensePurchaseId: String? = null,
        
        @field:SerializedName("address")
        val address: Any? = null,
        
        @field:SerializedName("profile_active")
        val profileActive: String? = null,
        
        @field:SerializedName("degree")
        val degree: String? = null,
        
        @field:SerializedName("lastname")
        val lastname: Any? = null,
        
        @field:SerializedName("phone")
        val phone: Any? = null,
        
        @field:SerializedName("name")
        val name: String? = null,
        
        @field:SerializedName("dobirth")
        val dobirth: Any? = null,
        
        @field:SerializedName("age")
        val age: Any? = null,
        
        @field:SerializedName("username")
        val username: String? = null,
        
        @field:SerializedName("status")
        val status: String? = null
        

        )

        【讨论】:

        • 我在 Main 类中使用 Response 或 DataItem 哪个 Model 类?
        • 只需将您的用户类替换为我编辑过的上述代码
        • 你也可以从 File->Settings->Plugins->Search RoboPOJOGenerate 安装 RoboPOJOGenerate 并安装.. 安装成功后,右键单击你的包->New->Generate POJO from Json(底部).. 只需复制粘贴您的 json 响应并生成类
        猜你喜欢
        • 2021-09-30
        • 2018-04-17
        • 2022-01-15
        • 1970-01-01
        • 1970-01-01
        • 2014-10-30
        • 2019-01-26
        • 2014-04-30
        • 1970-01-01
        相关资源
        最近更新 更多