我正在嘗試使用 Volley 從 API 獲取 JSON 物件,然后使用 MVVM Android 在 recyclerview 中顯示它。
在 ModelView (SchoolViewModel) 中使用 Volley jsonObjectRequest Get 請求來獲取通過 G??son 決議的我的資料 (JSON Object) 并將其傳遞給我的 LiveData,然后在我的 View (MainFragment) 中設定一個觀察者來觀察該資料并在回收商。
現在的問題是 Volley 只呼叫錯誤,“com.android.volley.ParseError: org.json.JSONException: Value [{"dbn":"02M260","school_name":"Clinton School...."
我不確定是什么導致了決議錯誤,因為它似乎正在檢索 JSON 物件,如錯誤所示。
任何想法為什么它總是跳到 VolleyError ?
我的觀點:
class MainFragment : Fragment() {
private lateinit var schoolViewModel: SchoolViewModel
private lateinit var linearLayoutManager: LinearLayoutManager
private var mainRecyclerAdapter: MainRecyclerAdapter = MainRecyclerAdapter()
//private var list: MutableList<MutableLiveData<SchoolsData>> = mutableListOf()
private var schoolsData = MutableLiveData<List<SchoolsData>>()
val API_Schools: String = "https://data.cityofnewyork.us/resource/s3k6-pzi2.json"
val API_SAT: String = "https://data.cityofnewyork.us/resource/f9bf-2cp4.json"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?) : View? {
// Inflate the layout for this fragment
val binding = FragmentMainBinding.inflate(layoutInflater)
schoolViewModel = ViewModelProvider(this).get(SchoolViewModel::class.java)
linearLayoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL,false)
context?.let { schoolViewModel.getData(it) }
schoolViewModel._schoolsData.observe(viewLifecycleOwner, Observer{
mainRecyclerAdapter.setData(it)
} )
binding.recyclerMain.apply {
layoutManager = linearLayoutManager
adapter = mainRecyclerAdapter
}
return binding.root
}
}
視圖模型
class SchoolViewModel: ViewModel() {
private var schoolsData = MutableLiveData<List<SchoolsData>>()
val _schoolsData: LiveData<List<SchoolsData>> = schoolsData
private var satData = MutableLiveData<SATData>()
val _satData: LiveData<SATData> = satData
lateinit var jsonObjectRequest: JsonObjectRequest
val API_Schools: String = "https://data.cityofnewyork.us/resource/s3k6-pzi2.json"
val API_SAT: String = "https://data.cityofnewyork.us/resource/f9bf-2cp4.json"
fun getData(context: Context) {
val requestQueue: RequestQueue = Volley.newRequestQueue(context.applicationContext)
jsonObjectRequest = JsonObjectRequest(Request.Method.GET,API_Schools, null,
{
try {
val gson = Gson()
schoolsData = gson.fromJson(
it.toString(),
MutableLiveData<List<SchoolsData>>()::class.java
)
Log.e("RECEIVED", schoolsData.toString())
}catch (e: JSONException){
Log.e("JSONException", e.toString())
}
}, {
Log.e("ERROR",it.toString())
})
requestQueue.add(jsonObjectRequest)
}
}
模型
import com.google.gson.annotations.SerializedName
data class SchoolsData(
@SerializedName("dbn")
val id: String,
@SerializedName("school_name")
val school_name: String,
@SerializedName("total_students")
val students_num: Int,
@SerializedName("graduation_rate")
val graduation_rate: Float,
@SerializedName("primary_address_line_1")
val street: String,
@SerializedName("city")
val city: String
)
uj5u.com熱心網友回復:
您要獲取的 JSON 是 JSONArray[{},{}]
而不是 JSONObject,因此您需要使用JsonArrayRequest
而不是JsonObjectRequest
. 一旦你做出改變,凌空錯誤就會消失,但你還需要更新決議代碼——你應該把它反序列化為一個List<SchoolsData>
,而不是一個 MutableLiveData,像這樣:
val gson = Gson()
val listType = object : TypeToken<List<SchoolsData>>() {}.type
val schoolList = gson.fromJson<List<SchoolsData>>(
it.toString(),
listType
)
schoolsData.postValue(schoolList)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/491869.html
上一篇:按月在美國所有州的Excel中獲得最高排名的最簡單方法?
下一篇:PHP決議帶有節點子節點的xml