Android API调用需要数组,获取对象
我有一个API调用,它寻找基本上在同一位置的数组和对象。我试图调用API,并得到以下错误。Android API调用需要数组,获取对象,android,retrofit2,android-recyclerview,Android,Retrofit2,Android Recyclerview,我有一个API调用,它寻找基本上在同一位置的数组和对象。我试图调用API,并得到以下错误。 io.reactivex.exceptions.OnErrorNotImplementedException:应为BEGIN\u数组,但在第1行第2列路径处为BEGIN\u对象$ 如何调用attributes类中的根项和子项 我在应用程序中进行了一些登录,URL是正确的,但问题在于响应 这是API- { "id": "65", "name": "Switch - Kitchen",
io.reactivex.exceptions.OnErrorNotImplementedException:应为BEGIN\u数组,但在第1行第2列路径处为BEGIN\u对象$
如何调用attributes类中的根项和子项
我在应用程序中进行了一些登录,URL是正确的,但问题在于响应
这是API-
{
"id": "65",
"name": "Switch - Kitchen",
"label": "Switch - Kitchen",
"attributes": [
{
"name": "switch",
"currentValue": "off",
"dataType": "ENUM",
"values": [
"on",
"off"
]
}
],
"capabilities": [
"Switch",
{
"attributes": [
{
"name": "switch",
"dataType": null
}
]
},
"Configuration",
"Refresh",
"Actuator"
],
"commands": [
"configure",
"flash",
"off",
"on",
"refresh",
"refresh"
]
}
使用json映射工具,这就是它为布局定义的内容
public class Attribute {
@SerializedName("name")
@Expose
private String name;
@SerializedName("currentValue")
@Expose
private String currentValue;
@SerializedName("dataType")
@Expose
private String dataType;
@SerializedName("values")
将它定义的类分开
public class Example {
@SerializedName("id")
@Expose
private String id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("label")
@Expose
private String label;
@SerializedName("attributes")
@Expose
private List<Attribute> attributes = null;
@SerializedName("capabilities")
@Expose
private List<String> capabilities = null;
@SerializedName("commands")
@Expose
private List
从您共享的JSON中,您的
INetworkAPIDetails
应该是:
interface INetworkAPIDetails {
@GET("devices/65")
fun getAllDetails(@Query("access_token") access_token: String): Observable<DeviceDetails>
}
接口详细信息{
@获取(“设备/65”)
fun getAllDetails(@Query(“访问令牌”)访问令牌:字符串):可观察
}
结构似乎是设备详细信息
,加上列表
意味着它需要一个对象数组,在更正之前需要一个属性
类型的对象数组
另外,在功能
数组中有一个棘手的情况:在您的数据结构中,定义为列表
。。。但是第二个元素“开关”
和“配置”
不是字符串
,而是一个完整的对象
对于这种情况,请参见或您确定API将返回数组吗?如果您转到此处并插入数组,它会将字符串和数组显示为值我这样做了,但现在我得到了错误“预期的字符串,但被赋予了对象”。好,现在我意识到在
功能
数组中有一个棘手的情况:在您的数据结构中定义为列表
。。。但是第二个元素“开关”
和“配置”
不是字符串
,而是一个完整的对象
。我很确定这就是现在的错误。对于这种情况,请参见或将其添加到答案中。是否有办法从数组中获取某些项,而不是获取所有项?据我所知,这对java非常不友好,需要一些技巧才能开始工作。所有可能的选择都意味着“弄脏你的手”。我链接的可能是最干净的。
data class Attribute(
val currentValue: String,
val dataType: String,
val name: String,
val values: List<String>
)
interface INetworkAPIDetails {
@GET("devices/65")
fun getAllDetails(@Query("access_token") access_token: String): Observable<List<Attribute>>
class PostItemDetailsAdapter(private val postdetailsList: List<Attribute>, private val context: Context) :
RecyclerView.Adapter<PostItemDetailsAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(
LayoutInflater.from(context).inflate(R.layout.post_item_details,
parent, false))
}
override fun getItemCount(): Int {
return postdetailsList.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.itemView.txtPostName.text = this.postdetailsList[position].name
holder.itemView.txtPostCurrent.text = this.postdetailsList[position].currentValue
holder.itemView.txtPostDataType.text = this.postdetailsList[position].dataType
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view)
}
class Activity2 : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity2)
//Debugging URL//
val interceptor : HttpLoggingInterceptor = HttpLoggingInterceptor().apply {
this.level = HttpLoggingInterceptor.Level.BODY
}
val client : OkHttpClient = OkHttpClient.Builder().apply {
this.addInterceptor(interceptor)
}.build()
//Debugging URL//
Log.d("TAG", "Activity 2")
val retrofit = Retrofit.Builder().addConverterFactory(GsonConverterFactory.create(GsonBuilder().create()))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl("http://xxx.xxx.xxx.xxx/apps/api/109/")
.client(client)
.build()
//Base of API Call//
//Build List of Devices//
val postsApi = retrofit.create(INetworkAPIDetails::class.java)
var response = postsApi.getAllDetails(access_token = "xxxx")
//Displaying List//
response.observeOn(AndroidSchedulers.mainThread()).subscribeOn(IoScheduler()).subscribe {
rv__list_posts_details.adapter = PostItemDetailsAdapter(it, this)
}
}
}
interface INetworkAPIDetails {
@GET("devices/65")
fun getAllDetails(@Query("access_token") access_token: String): Observable<DeviceDetails>
}