我试图检索数据并在回收器视图中显示它们。我正在进行改装,提出获取请求...一切都很好,除了:它不会在回收器中显示数据,因为它说:“预期BEGIN_ARRAY但在第1行第2列BEGIN_OBJECT”。我还使用了JSON插件中的kotlin数据类文件,从响应中生成数据类。我不知道该怎么办,我被卡住了。.
以下是我的课程:
ApiClient。kt
object ApiClient {
private const val BASE_URL: String = "https://dev.fastbeach.it/api/"
private val gson : Gson by lazy {
GsonBuilder().setLenient().create()
}
private val httpClient : OkHttpClient by lazy {
OkHttpClient.Builder().build()
}
private val retrofit : Retrofit by lazy {
Retrofit.Builder()
.baseUrl(BASE_URL)
.client(httpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
}
val apiService : ApiService by lazy{
retrofit.create(ApiService::class.java)
}
ApiService.kt
interface ApiService {
@GET("tenant/searchAllWithPagesAndFilters")
fun getUsers(
@Query("reservationEnabled") reservationEnabled:Boolean,
@Query("page") page:Int,
@Query("size") size:Int,
@Query("sort") sort:String,
@Query("direction") direction:String
): Call<MutableList<User>>
UserRepository.kt
object UserRepository {
fun getMutableLiveData(context: Context) : MutableLiveData<ArrayList<User>>{
val mutableLiveData = MutableLiveData<ArrayList<User>>()
context.showProgressBar()
ApiClient.apiService.getUsers(true,0,5,"order","ASC").enqueue(object : Callback<MutableList<User>> {
override fun onFailure(call: Call<MutableList<User>>, t: Throwable) {
hideProgressBar()
Log.e("error", t.localizedMessage)
}
override fun onResponse(
call: Call<MutableList<User>>,
response: Response<MutableList<User>>
) {
hideProgressBar()
val usersResponse = response.body()
usersResponse?.let { mutableLiveData.value = it as ArrayList<User> }
}
})
return mutableLiveData
}
用户视图模型。kt
class UserViewModel(private val context: Context) : ViewModel() {
private var listData = MutableLiveData<ArrayList<User>>()
init{
val userRepository : UserRepository by lazy {
UserRepository
}
if(context.isInternetAvailable()) {
listData = userRepository.getMutableLiveData(context)
}
}
fun getData() : MutableLiveData<ArrayList<User>>{
return listData
}
主要活动。kt
class MainActivity : AppCompatActivity() {
private lateinit var listUsers: MutableList<User>
private lateinit var adapter: UsersAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recycler_main.layoutManager = LinearLayoutManager(this@MainActivity)
listUsers = mutableListOf<User>()
adapter = UsersAdapter(this,
listUsers
)
recycler_main.adapter = adapter
val userViewModel = ViewModelProviders.of(this,UserViewModelFactory(this)).get(UserViewModel::class.java)
userViewModel.getData().observe(this,object:Observer<ArrayList<User>>{
override fun onChanged(t: ArrayList<User>?) {
listUsers.clear()
t?.let { listUsers.addAll(it) }
adapter.notifyDataSetChanged()
}
})
}
来自POSTMAN的API响应
{
"content": [
{
"id": "609d2d79be5f8e42d9c13e41",
"name": "bagno-sirena-posillipo",
"title": "Bagno Sirena Posillipo",
"tenantName": "Bagno Sirena",
"tenantCode": "BSI",
"description": "E' uno dei siti balneari più antichi, inserito in un posto incantevole a ridosso del Palazzo Donn'Anna, in una piccola insenatura riparata e quindi usufruibile in tutti i mesi dell'anno per chi vuole in piena città trascorrere qualche ora di relax respirando aria di mare e conservare l'abbronzatura estiva.\nI gestori si stanno impegnando per migliorare i servizi della location.\nVorremmo condividere con tutti gli amanti del mare e della costa di Posillipo questa meraviglia che abbiamo.\nVeniteci a trovare e avrete modo di constatare questa bellezza.",
"confirmBookingsRequired": false,
"paymentRequired": true,
"fee": 1.0,
"foodFee": 0.5,
"feePercentage": 0.02,
"foodFeePercentage": 0.02,
"percentage": true,
"signInSignOnEnabled": true,
"qrCodeValidation": true,
"tenantImage": {},
"tenantOtherImage": [],
"type": "Beach",
"enabledServices": [],
"master": false,
"comune": "Napoli",
"comuneId": "063049",
"address": {
"address": "Via Posillipo, 357",
"city": "Napoli",
"province": "NA",
"country": "Italia",
"postalNumber": "80123",
"phoneNumber": "081 769 2885",
"web": "https://www.facebook.com/bagnosirenanapoli/",
"email": "development@synclab.it"
},
"reservationEnabled": true,
"foodEnabled": true,
"reservationWithoutMapButWithCart": false,
"criteriaNumPlaces": {},
"supportedServices": [
{
"id": "5f04a23f968f9926b4b76746",
"name": "Lettini",
"description": "Lettini",
"supported": true,
"tenantTypes": []
},
{
"id": "5f04a240968f9926b4b76747",
"name": "Ombrelloni",
"description": "Ombrelloni",
"supported": true,
"tenantTypes": []
},
{
"id": "5f04a240968f9926b4b76748",
"name": "Sdraio",
"description": "Sdraio",
"supported": false,
"tenantTypes": []
},
{
"id": "5f04a240968f9926b4b76749",
"name": "Parcheggio",
"description": "Parcheggio",
"supported": false,
"tenantTypes": []
},
{
"id": "5f04a240968f9926b4b7674a",
"name": "Lettoni",
"description": "Lettoni",
"supported": false,
"tenantTypes": []
},
{
"id": "5f04a240968f9926b4b7674b",
"name": "Accessibilita",
"description": "Accesso disabili\n",
"supported": false,
"tenantTypes": []
},
{
"id": "5f04a240968f9926b4b7674c",
"name": "Area_baby",
"description": "Area baby",
"supported": false,
"tenantTypes": []
},
{
"id": "5f04a240968f9926b4b7674d",
"name": "Ristorante",
"description": "Ristorante",
"supported": true,
"tenantTypes": []
},
{
"id": "5f04a240968f9926b4b7674e",
"name": "Bar",
"description": "Bar",
"supported": false,
"tenantTypes": []
},
{
"id": "5f04a240968f9926b4b7674f",
"name": "Alcolici",
"description": "Alcolici",
"supported": false,
"tenantTypes": []
},
{
"id": "5f04a240968f9926b4b76750",
"name": "Gazebo",
"description": "Gazebo",
"supported": false,
"tenantTypes": []
},
{
"id": "5f04a240968f9926b4b76751",
"name": "Pagoda",
"description": "Pagoda",
"supported": false,
"tenantTypes": []
},
{
"id": "5f04a240968f9926b4b76752",
"name": "Wc",
"description": "Wc",
"supported": true,
"tenantTypes": []
},
{
"id": "60c72a1709e2081fa9b6d422",
"name": "Wi-fi",
"description": "Wi-fi",
"supported": false,
"tenantTypes": []
},
{
"id": "60c72a2509e2081fa9b6d423",
"name": "Accesso_animali",
"description": "Animali",
"supported": false,
"tenantTypes": []
},
{
"id": "60c72a2709e2081fa9b6d424",
"name": "Animazione",
"description": "Animazione",
"supported": false,
"tenantTypes": []
},
{
"id": "60c72a2a09e2081fa9b6d425",
"name": "Cabine",
"description": "Cabine",
"supported": false,
"tenantTypes": []
},
{
"id": "60c72a4109e2081fa9b6d426",
"name": "Piscina",
"description": "Piscina",
"supported": false,
"tenantTypes": []
},
{
"id": "60ec61341dbc2d4ed0d130ff",
"name": "Sedia_regista",
"description": "Sedia regista",
"supported": false,
"searchable": true,
"tenantTypes": []
}
],
"types": [
{
"typeId": "60c73af609e2081fa9b6d42a",
"name": "Sassi",
"description": "Sassi",
"supported": false,
"tenantTypes": []
},
{
"typeId": "60c73af609e2081fa9b6d42b",
"name": "Erba_sintetica",
"description": "Erba sintetica",
"supported": false,
"tenantTypes": []
},
{
"typeId": "60c73af609e2081fa9b6d42c",
"name": "Molo",
"description": "Molo",
"supported": false,
"tenantTypes": []
},
{
"typeId": "60ccb0874107e97fccaac087",
"name": "Spiaggia",
"description": "Spiaggia",
"supported": true,
"tenantTypes": []
},
{
"typeId": "60ccb0874107e97fccaac088",
"name": "Erba_naturale",
"description": "Erba naturale",
"supported": false,
"tenantTypes": []
},
{
"typeId": "60ccb0874107e97fccaac089",
"name": "Ciottoli",
"description": "Ciottoli",
"supported": false,
"tenantTypes": []
}
],
"targets": [
{
"id": "60c73af609e2081fa9b6d42d",
"name": "Famiglie",
"description": "Famiglie",
"supported": true,
"tenantTypes": []
},
{
"id": "60c73af609e2081fa9b6d42e",
"name": "Coppie",
"description": "Coppie",
"supported": true,
"tenantTypes": []
},
{
"id": "60c73af609e2081fa9b6d42f",
"name": "Ragazzi",
"description": "Ragazzi",
"supported": false,
"tenantTypes": []
}
],
"visible": true,
"maxNumBookingGuests": 0,
"numBookablePlaces": 0,
"maxPlacesPerBooking": 0,
"withoutLayout": false,
"lastMinute": true,
"upfront": false,
"latitude": 40.8197859,
"longitude": 14.2113269,
"order": 1,
"bookingUserDetailActive": false
}
],
"pageable": {
"sort": {
"sorted": true,
"unsorted": false,
"empty": false
},
"pageNumber": 0,
"pageSize": 1,
"offset": 0,
"paged": true,
"unpaged": false
},
"last": true,
"totalElements": 1,
"totalPages": 1,
"sort": {
"sorted": true,
"unsorted": false,
"empty": false
},
"first": true,
"number": 0,
"numberOfElements": 1,
"size": 1,
"empty": false
通过插件生成的数据类。(我就贴这个)
data class User(
@SerializedName("content")
val content: Content? = null,
@SerializedName("empty")
val empty: Boolean? = null,
@SerializedName("first")
val first: Boolean? = null,
@SerializedName("last")
val last: Boolean? = null,
@SerializedName("number")
val number: Int? = null,
@SerializedName("numberOfElements")
val numberOfElements: Int? = null,
@SerializedName("size")
val size: Int? = null,
@SerializedName("totalElements")
val totalElements: Int? = null,
@SerializedName("totalPages")
val totalPages: Int? = null
):可序列化
您需要的数据类型不是服务器提供的。实际上,endpoint会返回一个json对象,而您需要一个用户列表。
您需要创建另一个保存用户列表的数据类。
如果您在创建数据类时迷失了方向,我建议您使用JsonToKotlin Android Studio扩展来轻松实现它们。
正在学习如何在我的Android应用程序中使用改型,出现以下错误:如果可以,请提供帮助。非常感谢。 以下是我的代码:如果您也有能力,请帮助: 这是我的界面: 以下是我的建议:
我的java代码 我的JSON 除id外,所有字段均为字符串 我的错误 18/11/26 18:02:55信息mapreduce。作业:任务Id:尝试_1543189350698_0010_m_000000_0,状态:失败错误:com。谷歌。格森。JsonSyntaxException:java。伊奥。EOFEException:com第1行第3列的输入结束。谷歌。格森。格森。fromJson(G
当我只收到列表中的一个项目时,就会出现这个错误。我在服务器端REST Web服务中使用Jersey,只有当列表返回一个元素时,以及当它有但当它有多个异常时,它工作得非常完美,我就是这样做的: Jersey为什么不发送包含一个项目的列表?是泽西岛的虫子吗?有没有办法让它始终序列化为数组? 这是web服务返回0元素时的JSON数据: 这是web服务返回1个元素时的JSON数据: 当web服务返回超过1
这个错误又回来了! 我是改装的初学者,我看过这篇文章:改装:预期BEGIN_OBJECT但BEGIN_ARRAY 但是即使我试着让它适应我的模型,我仍然不能让它工作。所以我的错误是: 我得到的是: 解析类的属性: IoTGenericDevice子类的属性: 改装电话: 经理:
我试图用MovieGlu API返回数据,但当我运行应用程序时,我得到以下错误: 改装2:应为BEGIN_数组,但在第1行第2列路径处为BEGIN_对象$ 我希望能够在文本视图中一个接一个地显示单个对象的结果,例如:“film_name”:“赌场”,而不是一次显示所有对象。
我使用Laravel构建了一个简单的API,它运行良好。我的Android应用程序应该使用这个API中的数据,但对于一些JSON响应,我得到了一个错误 应为BEGIN_对象,但在第1行第1列路径处为字符串 在得到这个错误之前,错误如下: 使用JsonReader。setLenient(true)在第1行第1列路径接受格式错误的JSON 然后我来解决它。 我在许多其他帖子中看到,这个问题是一个畸形的