во-первых, извините за плохой английский, я пытаюсь сделать приложение, используя гистограмму, мне нужно получить данные для бара от Api, поэтому я использую модификацию для вызова данных и помещаю данные в гриф, но я застрял вгде, когда я звоню, данные являются нулевыми, но если я открываю веб-интерфейс 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
)
и это setDataBar () на моем IRFragmentUnit_1K.kt
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",
},
]