Using Retrofit, I just Call a GET Request from an endpoint successfully. It returns the proper response in the response.body()
. In the Logcat, it is like [this][1].
The response look like this in postman: [![enter image description here][2]][2]
Now what i want to extract from response.body
is that Header
attribute and pass them to some textviews. As you can see above, it is a single object.
All examples in the internet of how to use retrofit is using it to populate a recyclerview. As for this, it is a single object and i want to show them not in a listview or a recyclerview.
Here's the snippet code of the layout where i want to show them (It looks like this):
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginStart="32dp"
android:layout_marginTop="24dp">
<TextView
android:id="@+id/detailOutletName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="XXX"
android:textSize="16sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/detailOrderNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="XXX"
android:textSize="16sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
<TextView
android:id="@+id/detailOrderDateTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="XXX"
android:textSize="16sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
<TextView
android:id="@+id/dtailOrderTotalPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="XXX"
android:textSize="16sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
<TextView
android:id="@+id/detailCustomerName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="XXX"
android:textSize="16sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
<TextView
android:id="@+id/detailCustomerAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="XXX"
android:textSize="16sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
<TextView
android:id="@+id/detailCustomerPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="XXX"
android:textSize="16sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
</LinearLayout>
And this is the data class I use:
data class OutstandingOrderDetailPOJODataClass(
@field:SerializedName("data")
val data: OutstandingOrderDetailPOJODataClassData? = null,
@field:SerializedName("error")
val error: Error? = null
)
data class OutstandingOrderDetailPOJODataClassData(
@field:SerializedName("Header")
val header: OutstandingOrderDetailPOJODataClassHeader? = null,
@field:SerializedName("Detail")
val detail: List<OutstandingOrderDetailPOJODataClassDetailItem?>? = null
)
data class OutstandingOrderDetailPOJODataClassHeader(
@field:SerializedName("buyer_address")
val buyerAddress: String? = null,
@field:SerializedName("total_price")
val totalPrice: Int? = null,
@field:SerializedName("buyer_name")
val buyerName: String? = null,
@field:SerializedName("status_confirmed_yn")
val statusConfirmedYn: String? = null,
@field:SerializedName("order_date")
val orderDate: String? = null,
@field:SerializedName("outlet_id")
val outletId: String? = null,
@field:SerializedName("nip")
val nip: String? = null,
@field:SerializedName("jumlah_product")
val jumlahProduct: Int? = null,
@field:SerializedName("last_update")
val lastUpdate: String? = null,
@field:SerializedName("phone_number")
val phoneNumber: String? = null,
@field:SerializedName("order_running_id")
val orderRunningId: Int? = null,
@field:SerializedName("status_tagged_yn")
val statusTaggedYn: String? = null,
@field:SerializedName("order_id")
val orderId: String? = null
)
Here's the code snippet from the fragment of the layout:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val args = arguments?.let { OrderDetailFragmentArgs.fromBundle(it) }
val selectedOrderId = args?.orderId
// Change action bar title
(activity as AppCompatActivity).supportActionBar?.title = "Order Details"
// View Binding for this Fragment
binding = DataBindingUtil.inflate(
inflater,
R.layout.fragment_order_detail, container, false
)
if (selectedOrderId != null) {
fetchOrderDetail(selectedOrderId)
fetchOrderedItemListData(selectedOrderId)
}
//Inflate layout to this activity fragment
return binding.root
}
private fun fetchOrderDetail(selectedOrderId : String){
NetworkConfig()
.getOutstandingDetailService()
.getOutstandingOrderDetail(selectedOrderId)
.enqueue(object :
Callback<OutstandingOrderDetailPOJODataClass>{
override fun onFailure(
call: Call<OutstandingOrderDetailPOJODataClass>,
t: Throwable
) {
....
}
override fun onResponse(
call: Call<OutstandingOrderDetailPOJODataClass>,
response: Response<OutstandingOrderDetailPOJODataClass>
) {
Log.i("Order", "Order Detail fetched! -> \n ${response.body()}") //This line is how the response.body() shown in the logcat.
}
})
}
Is there a way to do achieve that ? If there's any detail that i miss to point out, feel free to ask.
In order to access the Header
part of the response, you need to access it like response.body().data?.header
or do it like below
val responseData = response.body().data
//now you can get any values inside the data object as like below
val header = responseData?.header
val attributeName = header?.attributeName