首先对不起,英语不好,我尝试使用条形图制作一个应用程序,我必须从Api获取条形图的数据,因此我使用翻新方法来调用该数据并将其放在条形上,但我坚持使用我在哪里调用数据为null,但是如果我打开api网络,它将显示在浏览器上
这是我的ApiService.kt
import com.abisayuti.coalyardstock.service.IssueRecieve.ResponseIssue
import retrofit2.Call
import retrofit2.http.GET
interface ApiService {
@GET("2019/coal/index.php/api/monitoring/shipping/")
fun fetchAllShipping(): Call<List<ResponseCS1k>>
@GET("2019/coal/index.php/api/monitoring/issue_stock?unit=u12")
fun fetchAllCoal(): Call<ResponseIssue>
}
这是我的ResponseIssue.kt
package com.abisayuti.coalyardstock.service.IssueRecieve
import com.google.gson.annotations.SerializedName
data class ResponseIssue(
@field:SerializedName("name_coal")
val nameCoal: String? = null,
@field:SerializedName("qty_stock")
val qtyStock: String? = null,
@field:SerializedName("name_vendor")
val nameVendor: String? = null,
@field:SerializedName("timestamp")
val timestamp: String? = null
)
这是我IRFragmentUnit_1K.kt上的setDataBar()
internal fun setDataBar() {
val httpClient = OkHttpClient.Builder()
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
httpClient.interceptors().add(interceptor)
val gson = GsonBuilder()
.setLenient()
.create()
val retrofit = Retrofit.Builder()
.baseUrl(baseurl)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
val service = retrofit.create(ApiService::class.java)
val call = service.fetchAllCoal()
call.enqueue(object : Callback<ResponseIssue> {
override fun onFailure(call: Call<ResponseIssue>, t: Throwable) {
Log.d("onFailure", "onFailure ${t.message}")
}
override fun onResponse(call: Call<ResponseIssue>, response: Response<ResponseIssue>) {
val respon = response.body()!!
val y = respon.qtyStock.toString()
val name = respon.nameVendor.toString()
Log.d("OnResponse", "ada ${name}")
Log.d("onResponse", "ada ${y}")
val bargroup = ArrayList<BarEntry>()
val bargroup2 = ArrayList<BarEntry>()
val bargroup3 = ArrayList<BarEntry>()
val bargroup4 = ArrayList<BarEntry>()
val bargroup5 = ArrayList<BarEntry>()
val bargroup6 = ArrayList<BarEntry>()
val bargroup7 = ArrayList<BarEntry>()
bargroup.add(BarEntry(0f, 0f, name))
bargroup2.add(BarEntry(1f, 0f, name))
bargroup3.add(BarEntry(2f, 0f, name))
bargroup4.add(BarEntry(3f, 0f, name))
bargroup5.add(BarEntry(4f, 0f, name))
bargroup6.add(BarEntry(5f, 0f, name))
bargroup7.add(BarEntry(6f, 0f, name))
// creating dataset for Bar Group
val barDataSet = BarDataSet(bargroup, name)
val barDataSet2 = BarDataSet(bargroup2, name)
val barDataSet3 = BarDataSet(bargroup3, name)
val barDataSet4 = BarDataSet(bargroup4, name)
val barDataSet5 = BarDataSet(bargroup5, name)
val barDataSet6 = BarDataSet(bargroup6, name)
val barDataSet7 = BarDataSet(bargroup7, name)
val data =
BarData(barDataSet, barDataSet2, barDataSet3, barDataSet4, barDataSet5, barDataSet6, barDataSet7)
val textSize = "10"
bar_chart_unit3.data = data
bar_chart_unit3.xAxis.position = XAxis.XAxisPosition.BOTTOM
bar_chart_unit3.xAxis.labelCount = 11
bar_chart_unit3.xAxis.enableGridDashedLine(5f, 5f, 0f)
bar_chart_unit3.axisRight.enableGridDashedLine(5f, 5f, 0f)
bar_chart_unit3.axisLeft.enableGridDashedLine(5f, 5f, 0f)
bar_chart_unit3.description.isEnabled = false
bar_chart_unit3.animateY(1000)
bar_chart_unit3.legend.isEnabled = true
bar_chart_unit3.legend.textSize = textSize.toFloat()
bar_chart_unit3.setPinchZoom(true)
bar_chart_unit3.data.setDrawValues(false)
}
})
Logcat中的显示此
D/OnResponse: ada null
D/onResponse: ada null
这是我的回复json
[
{
qty_stock: "500",
name_vendor: "BUKIT ASAM",
name_coal: "COAL YARD I",
timestamp: "2019-03-01",
},
{
qty_stock: "150",
name_vendor: "BUKIT ASAM",
name_coal: "COAL YARD I",
timestamp: "2019-03-02",
},
{
qty_stock: "20",
name_vendor: "JORONG",
name_coal: "COAL YARD I",
timestamp: "2019-03-01",
},
{
qty_stock: "400",
name_vendor: "SSP",
name_coal: "COAL YARD IV",
timestamp: "2019-03-01",
},
]
答案 0 :(得分:0)
您的响应是一个数组而不是对象。
按如下所示更改界面
@GET("2019/coal/index.php/api/monitoring/issue_stock?unit=u12")
fun fetchAllCoal(): Call<List<ResponseIssue>>
}
然后在您的onResponse
中获得如下所示的变量。
val respon = response.body()!!
val y = respon[0].qtyStock.toString(),
val name = respon[0].nameVendor.toString()
或使用for循环迭代arraylist中的所有值。
答案 1 :(得分:0)
internal fun setDataBar() {
val httpClient = OkHttpClient.Builder()
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
httpClient.interceptors().add(interceptor)
val gson = GsonBuilder()
.setLenient()
.create()
val retrofit = Retrofit.Builder()
.baseUrl(baseurl)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
val service = retrofit.create(ApiService::class.java)
val call = service.fetchAllCoal()
call.enqueue(object : Callback<List<ResponseIssue>> {
override fun onFailure(call: Call<List<ResponseIssue>>, t: Throwable) {
Log.d("Response", "${t.message}")
}
override fun onResponse(call: Call<List<ResponseIssue>>, response: Response<List<ResponseIssue>>) {
val respon = response.body()!!
val y = respon[0].qtyStock!!.toFloat()
val name = respon[0].nameVendor.toString()
Log.d("Response", "Name ${name}")
Log.d("Response", "Y ${y}")
}
}) }