I am successfully able to hit the API and get the json result. I can see the success result in the logs by printing Retrofit response body. and also using Stetho as the network interceptor.
However, I am not able to understand why is the api response still "null" in the onResponse() method in the repository. I believe, I am not passing the correct model maybe for the JSON to be parsed properly ? Can anybody help me to find out what's the issue here?
Following is the json:
{
"photos": {
"page": 1,
"pages": 2864,
"perpage": 100,
"total": "286373",
"photo": [
{
"id": "49570734898",
"owner": "165034061#N07",
"secret": "f3cb2c2590",
"server": "65535",
"farm": 66,
"title": "Hello",
"ispublic": 1,
"isfriend": 0,
"isfamily": 0
}
],
"photo": [
{
"id": "12344",
"owner": "23444#N07",
"secret": "f233edd",
"server": "65535",
"farm": 66,
"title": "Hey",
"ispublic": 1,
"isfriend": 0,
"isfamily": 0
}
]
},
"stat": "ok"
}
My Pojo Class :
data class Photos(
#SerializedName("page")
val page: Int,
#SerializedName("pages")
val pages: Int,
#SerializedName("perpage")
val perpage: Int,
#SerializedName("photo")
val photos: List<Photo>,
#SerializedName("total")
val total: String
)
data class Photo(
#SerializedName("farm")
val farm: Int,
#SerializedName("id")
val id: String,
#SerializedName("isfamily")
val isFamily: Int,
#SerializedName("isfriend")
val isFriend: Int,
#SerializedName("ispublic")
val isPublic: Int,
#SerializedName("owner")
val owner: String,
#SerializedName("secret")
val secret: String,
#SerializedName("server")
val server: String,
#SerializedName("title")
val title: String
)
RetrofitClient:
object ApiClient {
private val API_BASE_URL = "https://api.flickr.com/"
private var servicesApiInterface: ServicesApiInterface? = null
fun build(): ServicesApiInterface? {
val builder: Retrofit.Builder = Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
val httpClient: OkHttpClient.Builder = OkHttpClient.Builder()
httpClient.addInterceptor(interceptor()).addNetworkInterceptor(StethoInterceptor())
val retrofit: Retrofit = builder
.client(httpClient.build()).build()
servicesApiInterface = retrofit.create(
ServicesApiInterface::class.java
)
return servicesApiInterface as ServicesApiInterface
}
private fun interceptor(): HttpLoggingInterceptor {
val httpLoggingInterceptor = HttpLoggingInterceptor()
httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
return httpLoggingInterceptor
}
interface ServicesApiInterface {
#GET("/services/rest/?method=flickr.photos.search")
fun getImageResults(
#Query("api_key") apiKey: String,
#Query("text") text: String,
#Query("format") format: String,
#Query("nojsoncallback") noJsonCallback: Boolean
): Call<PhotoResponse>
}
}
OperationCallback:
interface OperationCallback<T> {
fun onSuccess(data:List<T>?)
fun onError(error:String?)
}
PhotoDataSource:
interface PhotoDataSource {
fun retrievePhotos(callback: OperationCallback<Photo>, searchText: String)
fun cancel()
}
PhotoRepository:
class PhotoRepository : PhotoDataSource {
private var call: Call<PhotoResponse>? = null
private val API_KEY = "eff9XXXXXXXXXXXXX"
val FORMAT = "json"
companion object {
val TAG = PhotoRepository::class.java.simpleName
}
override fun retrievePhotos(callback: OperationCallback<Photo>, searchText: String) {
call = ApiClient.build()
?.getImageResults(
apiKey = API_KEY,
text = searchText,
format = FORMAT,
noJsonCallback = true
)
call?.enqueue(object : Callback<PhotoResponse> {
override fun onFailure(call: Call<PhotoResponse>, t: Throwable) {
callback.onError(t.message)
}
override fun onResponse(
call: Call<PhotoResponse>,
response: Response<PhotoResponse>
) {
response?.body()?.let {
Log.d(TAG, "got api response total pics are :${it.data?.size}")
if (response.isSuccessful && (it.isSuccess())) {
callback.onSuccess(it.data)
} else {
callback.onError(it.msg)
}
}
}
})
}
override fun cancel() {
call?.let {
it.cancel()
}
}
}
PhotoResponse:
data class PhotoResponse(val status: Int?, val msg: String?, val data: List<Photo>?) {
fun isSuccess(): Boolean = (status == 200)
}
Try to change your PhotoResponse to match with your json response.
data class PhotoResponse(
#SerializedName("stat")
val status: String?,
#SerializedName("photos")
val photos: Photos?
) {
fun isSuccess(): Boolean = status.equals("ok", true)
}
And then inside onResponse, You can get List<Photo> like below:
override fun onResponse(
call: Call<PhotoResponse>,
response: Response<PhotoResponse>
) {
response?.body()?.let {
//This should be your list of photos
it.photos.photos
}
}
The issue is with your data class. You need one extra data class here.
So if you look at your JSON response closely, then you will understand whats going wrong.
Your photos data class should not be the first class. Instead it should be inside one more class lets say PhotoApiResponse.
Your first class will contain both photos and stat.
And then rest can be the same.
Related
I have problem with my project. It is simple connection with api application. I want to take information like this:
{
"docs": [
{
"_id": "5cf5805fb53e011a64671582",
"name": "The Fellowship Of The Ring"
},
{
"_id": "5cf58077b53e011a64671583",
"name": "The Two Towers"
},
{
"_id": "5cf58080b53e011a64671584",
"name": "The Return Of The King"
}
],
"total": 3,
"limit": 1000,
"offset": 0,
"page": 1,
"pages": 1
}
But I got illegalStateException like in title. Theres is my files:
DTO
data class CharacterDTO(
val docs: List<CharacterData>,
val limit: Int,
val offset: Int,
val page: Int,
val pages: Int,
val total: Int
)
data class CharacterData(
val _id: String,
val birth: String,
val death: String,
val gender: String,
val hair: String,
val height: String,
val name: String,
val race: String,
val realm: String,
val spouse: String,
val wikiUrl: String
)
fun CharacterData.toCharacter(): Character{
return Character(
_id = _id,
birth = birth,
death = death,
gender = gender,
name = name,
race = race
)
}
And it's crash on this file:
USECASE
lass GetCharactersUseCase #Inject constructor(
private val repository: LotrRepository
) {
operator fun invoke(): Flow<List<Character>> = flow {
try {
val characters = repository.getCharacters() // on this line it crash
Log.d(Tag,characters.toString())
}catch (e: HttpException){
Log.d(Tag,e.toString())
}catch (e: IOException){
Log.d(Tag,e.toString())
}
}
}
And this is repository
interface LotrRepository {
suspend fun getBooks(): List<BookDTO>
suspend fun getCharacters(): List<CharacterDTO>
suspend fun getCharacterById(characterId: String): CharacterDTO
}
And last this is my request:
#Provides
#Singleton
fun provideLotrApi(): LotrApi{
val request = Retrofit.Builder()
.client(OkHttpClient.Builder().addInterceptor { chain ->
val request = chain.request().newBuilder().addHeader("Authorization", "Bearer ${Constants.API_KEY}").build()
chain.proceed(request)
}.build())
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(LotrApi::class.java)
return request
}
After my research i forgot to handle with this part
"total": 3,
"limit": 1000,
"offset": 0,
"page": 1,
"pages": 1
But after changes my DTO file it's still don't work
JSON provided above contains only a single CharacterDTO object, but you expected to get a list of such objects. Either change the backend to return a list or replace getCharacters() declaration with:
suspend fun getCharacters(): CharacterDTO
I am trying to get response body from this url:http:"//192.168.0.220:8000/records/?account_id=2"
In android studio i get status 200 but body is always null. Can anybody please tell me what I am doing wrong?
Response in postman looks like this:
{
"result": [
{
"id": 1,
"account_id": 2,
"title": "ez",
"datetime": "2021-03-21T00:00:00",
"description": "ez2",
"image": null,
"recording": null
},
{
"id": 2,
"account_id": 2,
"title": "ez",
"datetime": "2021-03-21T00:00:00",
"description": "ez2",
"image": null,
"recording": null
},
....
Response in android studio:
I/System.out: Response{protocol=http/1.1, code=200, message=OK, url=http://192.168.0.220:8000/records/?account_id=2}
Item(id=null, account_id=null, title=null, datetime=null, image=null, recording=null)
Interface:
interface Gett {
#GET("?account_id=2")
fun getRecord(): Call<Record.Item>
}
Class:
class Record {
data class Item(
val id: String,
val account_id: String,
val title: String,
val datetime: String,
val image: String,
val recording: String
)
}
MainActivity:
val retrofit = Retrofit.Builder()
.baseUrl("http://192.168.0.220:8000/records/")
.addConverterFactory(GsonConverterFactory.create())
.build()
val service = retrofit.create(Gett::class.java)
val call = service.getRecord()
call.enqueue(object : retrofit2.Callback<Record.Item> {
override fun onResponse(call: Call<Record.Item>, response: Response<Record.Item>) {
if (response.code() == 200) {
println(response)
println(response.body()!!)
}
}
override fun onFailure(call: Call<Record.Item>, t: Throwable) {
println("fail")
}
})
The issue might be this
interface Gett {
#GET("?account_id=2")
fun getRecord(): Call<Record.Item> <----
}
So, change your model
data class Record (
val result: List<Item>
)
data class Item(
val id: String,
val account_id: String,
val title: String,
val datetime: String,
val image: String,
val recording: String
)
As I can see your JSON has an array of your Item so change it to.
interface Gett {
#GET("?account_id=2")
fun getRecord(): Call<Record>
}
Thanks, #Kunn for pointing out JSONObject.
I haven't written anything here for a long time, but now I really need advice )
I'm using Retrofit2 as api client. Server API has one endpoint, for example /api/stats which receive JSON body request and return JSON response as:
data class StatsResult<T>(
#SerializedName("code") val code: Int,
#SerializedName("message") val msg: String?,
#SerializedName("request_id") val requestId: String?,
#SerializedName("data") val data: T?
)
If some error, data is null.
Otherwise data is an array that can contain different type of data depending on the type of request.
For example:
Request1:
{
"type": "type1",
"params": {
}
}
Response:
{
"code": 0,
"request_id": "...",
"data": [
{
"key1": "value1",
"key2": "value2"
},
{
"key1": "value3",
"key2": "value4"
}
]
}
Request2:
{
"type": "type2",
"params": {
}
}
Response:
{
"code": 0,
"request_id": "...",
"data": [
{
"key3": "value1",
"key4": "value2"
},
{
"key3": "value3",
"key4": "value4"
}
]
}
Here is my implementation in short:
interface StatsApi {
#POST("/api/stats")
suspend fun getStats(#Body request: StatsRequest): ApiResponse<StatsData>
}
sealed class ApiResponse<out T: Any> {
data class Success<T: Any>(val body: T): ApiResponse<T>()
object Unauthorized : ApiResponse<Nothing>()
object Forbidden: ApiResponse<Nothing>()
object NetworkError: ApiResponse<Nothing>()
data class Error(val msg: String? = null): ApiResponse<Nothing>()
data class Exception(val t: Throwable): ApiResponse<Nothing>()
}
typealias StatsData = StatsResult<List<BaseStatsDto>>
open class BaseStatsDto()
class Type1StatsDto: BaseStatsDto() {
#SerializedName("key1") var key1: String? = null
#SerializedName("key2") var key2: String? = null
}
class Type2StatsDto: BaseStatsDto() {
#SerializedName("key3") var key3: String? = null
#SerializedName("key4") var key4: String? = null
}
So I tried to workaround this with open/abstract class BaseStatsDto and than cast it to final class. But this solution didn't work.
For response handling I'm using CallAdapter.Factory() with custom Call<>:
open class ApiResponseCall<T : Any>(
private val delegate: Call<T>
) : Call<ApiResponse<T>> {
override fun enqueue(callback: Callback<ApiResponse<T>>) {
return delegate.enqueue(object : Callback<T> {
override fun onFailure(call: Call<T>, t: Throwable) {
val rsp = when (t) {
is IOException -> ApiResponse.NetworkError
else -> ApiResponse.Exception(t)
}
callback.onResponse(this#ApiResponseCall, Response.success(rsp))
}
override fun onResponse(call: Call<T>, response: Response<T>) {
val rsp: ApiResponse<T>
rsp = if (response.isSuccessful) {
val body = response.body()
ApiResponse.Success(body as T)
} else {
val code = response.code()
val error = response.errorBody()
when (code) {
401 -> ApiResponse.Unauthorized
403 -> ApiResponse.Forbidden
in 400..499 -> ApiResponse.Error("Client error")
in 500..599 -> ApiResponse.Error("Server error")
else -> ApiResponse.Exception(Exception("Unknown error"))
}
}
callback.onResponse(this#ApiResponseCall, Response.success(rsp))
}
})
}
...
}
I see another solution - to have separate interface functions with separate response types. And it working fine:
#POST("/api/stats")
suspend fun getType1Stats(#Body request: StatsRequest): ApiResponse<StatsResult<List<Type1StatsDto>>>
#POST("/api/stats")
suspend fun getType2Stats(#Body request: StatsRequest): ApiResponse<StatsResult<List<Type2StatsDto>>>
But if statistic data types count increases it will be very uncomfortable to maintain.
I would like to have one statistic api endpoint.
Use http://www.jsonschema2pojo.org/ Set SourceType: JSON and Annonation style: Gson
Data for request 1
class Data1 {
#SerializedName("key1")
#Expose
var key1: String? = null
#SerializedName("key2")
#Expose
var key2: String? = null
}
Response of request1
class Response1 {
#SerializedName("code")
#Expose
var code: Int? = null
#SerializedName("request_id")
#Expose
var requestId: String? = null
#SerializedName("data")
#Expose
var data: List<Data1>? = null
}
Data for request 2
class Data2 {
#SerializedName("key3")
#Expose
var key3: String? = null
#SerializedName("key4")
#Expose
var key4: String? = null
}
Response of request 2
class Response1 {
#SerializedName("code")
#Expose
var code: Int? = null
#SerializedName("request_id")
#Expose
var requestId: String? = null
#SerializedName("data")
#Expose
var data: List<Data2>? = null
}
with
#POST("/api/stats")
suspend fun getStats1(#Body request: StatsRequest1): ApiResponse<Response1>
#POST("/api/stats")
suspend fun getStats2(#Body request: StatsRequest2): ApiResponse<Response2>
help me for this issue please, I want to get data from API
{
"status": true,
"data": [
{
"id_pelanggan": "456",
"nama_pelanggan": "ahmad",
"alamat": "taliwang"
},
{
"id_pelanggan": "457",
"nama_pelanggan": "ahmad",
"alamat": "taliwang"
}
]}
this is my API object for setup for dynamic class, retrofit2 and gson
object Api {
private val BASE_URL: String = BuildConfig.API_SRAPP
private var gson = GsonBuilder().setLenient().create()
private val httpClient = OkHttpClient.Builder()
fun <T>service(java: Class<T>): T{
val retrofit = Retrofit.Builder()
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(BASE_URL)
.client(httpClient.build())
.build()
return retrofit.create(java)
}
}
this is my class BaseRespon for handle all data from API and with dynamic class
data class BaseResponse<T>(
val status: Boolean,
val data: T?
)
this is my class Customer
data class Customer (
#SerializedName("id_pelanggan")
#Expose
val idPelanggan: String,
#SerializedName("nama_pelanggan")
#Expose
val namaPelanggan: String,
#SerializedName("alamat")
#Expose
val alamat: String
)
this is my API service
interface CustomerServices {
#GET("customer")
fun getAllCustomer(#Header("Authorization") auth: String): Call<BaseResponse<ArrayList<Customer>>>
#GET("customer")
fun getCustomerbyID(#Header("Authorization") auth: String, #Query("id") id: String): Call<BaseResponse<Customer>>
}
and this is my class for using API
class CustomerPresenter {
fun loadAllCustomer(){
apiCustomer.getAllCustomer(OfflineHelper.getToken())
.enqueue(object : Callback<BaseResponse<ArrayList<Customer>>>{
override fun onFailure(call: Call<BaseResponse<ArrayList<Customer>>>, t: Throwable) {
Log.e("allCustomer", "${t.message}")
}
override fun onResponse(
call: Call<BaseResponse<ArrayList<Customer>>>,
response: Response<BaseResponse<ArrayList<Customer>>>
) {
saveLocalCustomer(response.body())
}
})
}
fun saveLocalCustomer(data: BaseResponse<ArrayList<Customer>>?){
Log.w("loadedAll", "${data?.status}")
}
and I have log failure in loadAllCustomer
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 2 column 1 path $
I don't understand why it's failure, my endpoint is not typo, and I have tested it on postman
You need to use a List as your return type for the data field. try this:
data class BaseResponseList<T>(
val status: Boolean,
val data: List<T>?
)
and then using BaseResponseList in your call as following:
#GET("customer")
fun getAllCustomer(#Header("Authorization") auth: String): Call<BaseResponseList<Customer>>
Use BaseResponseList for any endpoint that returns a List, and BaseResponse for Objects
Update your response class and api interface like below
data class BaseResponse(
val status: Boolean,
val data: List<Customer>?
)
interface CustomerServices {
#GET("customer")
fun getAllCustomer(#Header("Authorization") auth: String): Call<BaseResponse>
#GET("customer")
fun getCustomerbyID(#Header("Authorization") auth: String, #Query("id") id: String): Call<Customer>
}
your model not same with you object json
i am beginner in android dev (both java or kotlin). I am trying to populate spinner from json with retrofit and moshi but I have no idea how to populate it into spinner. to be honest I dont know whether the return of Json data is correct or not, since Log.d() return is not detail as dump() laravel or php.
script in activity onCreate (Please read the comment of the script, i put debug result of Log.d() there):
val task = object : AsyncTask<Void, Void, Response<List<ProductTypeResponse>>>() {
override fun doInBackground(vararg params: Void): Response<List<ProductTypeResponse>> {
val typeAPI = RestAPI()
val callResponse = typeAPI.getNews()
val response = callResponse.execute()
return response
}
override fun onPostExecute(response: Response<List<ProductTypeResponse>>) {
if (response.isSuccessful) {
val news = response.body()
Log.d("test:", news!![0].data.toString()) // method 'java.lang.String com.example.mockie.tigaer.api.TypeDataResponse.toString()' on a null object reference
Log.d("test:", news!!.size.toString()) // it says 67 but the data from the url is 63 array of json object
Log.d("test:", news!![0].toString()) // com.example.mockie.tigaer.api.ProductTypeResponse#f17fd5e
}
}
RestApi.kt
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
class RestAPI() {
private val tigaerApi: TigaerApi
init {
val retrofit = Retrofit.Builder()
.baseUrl("http://app.tigaer.id/laravel/")
.addConverterFactory(MoshiConverterFactory.create())
.build()
tigaerApi = retrofit.create(TigaerApi::class.java)
}
fun getNews(): Call<List<ProductTypeResponse>> {
return tigaerApi.getTop()
}
}
ApiModel.kt
package com.example.mockie.tigaer.api
class ProductTypeResponse(val data: TypeDataResponse)
class TypeDataResponse(
val children: List<ProductTypeChildrenResponse>
)
class ProductTypeChildrenResponse(val data: ProductTypeDataResponse)
class ProductTypeDataResponse(
val productType: String,
val readable: String
)
TigaerApi.kt
import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Query
interface TigaerApi {
#GET("api/type")
fun getTop(): Call<List<ProductTypeResponse>>
}
return Json: https://jsoneditoronline.org/?id=ce90c41b859218e746e41d64eddb4c30
so my questions are :
is there any function to debug object/array as detail as in laravel ?
how to populate my json return data into spinner?
Here is code for same, I modified and integrate in your code only:
"MainActivity.kt" class:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var spinner: Spinner = findViewById(R.id.spinner)
val task = object : AsyncTask<Void, Void, Response<List<ProductTypeDataResponse>>>() {
override fun doInBackground(vararg params: Void): Response<List<ProductTypeDataResponse>> {
val typeAPI = RestAPI()
val callResponse = typeAPI.getNews()
val response = callResponse.execute()
return response
}
override fun onPostExecute(response: Response<List<ProductTypeDataResponse>>) {
if (response.isSuccessful) {
val news: List<ProductTypeDataResponse>? = response.body()
var adapter: SpinnerAdapter = SpinnerAdapter(this#MainActivity, news!!);
spinner.adapter=adapter
}
}
}.execute()
}
}
Now Layout "activity_main":
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ankitpatidar.checkkotlin.MainActivity">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner"></Spinner>
</LinearLayout>
Now Spinner Adapter as "SpinnerAdapter":
class SpinnerAdapter internal constructor(internal var context: Context, internal var list: List<ProductTypeDataResponse>) : BaseAdapter() {
override fun getCount(): Int {
return list.size
}
override fun getItem(i: Int): Any? {
return null
}
override fun getItemId(i: Int): Long {
return 0
}
override fun getView(i: Int, view: View?, viewGroup: ViewGroup): View {
var view = view
if (view == null) {
val inflater = LayoutInflater.from(context)
view = inflater.inflate(R.layout.item, viewGroup, false)
}
val textView = view!!.findViewById<TextView>(R.id.textView)
textView.text = list[i].productType + " " + list[i].readable
return textView
}
}
Spinner item layout as "item":
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"/>
</LinearLayout>
Now some changes in your existing files:
"ApiModel.kt":
class TypeDataResponse(
val children: List<ProductTypeChildrenResponse>
)
class ProductTypeChildrenResponse(val data: ProductTypeDataResponse)
class ProductTypeDataResponse(
val productType: String,
val readable: String
)
"RestAPI.kt"
class RestAPI() {
private val tigaerApi: TigaerApi
init {
val retrofit = Retrofit.Builder()
.baseUrl("http://app.tigaer.id/laravel/")
.addConverterFactory(MoshiConverterFactory.create())
.build()
tigaerApi = retrofit.create(TigaerApi::class.java)
}
fun getNews(): Call<List<ProductTypeDataResponse>> {
return tigaerApi.getTop()
}
}
Hence it will work for you.
is there any function to debug object/array as detail as in laravel ?
Go through this to run your app in debug mode. https://developer.android.com/studio/debug/index.html
You can always use breakpoints to evaluate expressions while the app is running. Instead of logging put a breakpoint at this line.
val news = response.body()
so when you'll receive a response from server, app will stop at this point and you can check what you are getting in response in detail.
how to populate my json return data into spinner?
If you are getting response from server in Json format as shown in provided link, you'll have to parse the data into a list of objects(POJO).
And then you have to forward this data(maybe you'll have to iterate over list to get the required data because you have two fields in each object) into an adapter and set that adapter to your spinner. It is explained very clearly at following link.
https://developer.android.com/guide/topics/ui/controls/spinner.html
I got an idea in this post, maybe my logic can help anyone in here
this is my JSON
{
"success": 1,
"dataset": [
{
"id": "3",
"nama": "Rush"
},
{
"id": "5",
"nama": "Avanza"
},
{
"id": "6",
"nama": "Innova"
},
{
"id": "14",
"nama": "Sienta"
},
{
"id": "15",
"nama": "Alphard"
},
{
"id": "16",
"nama": "Calya"
}
],
"sql_duration": 0.0013179779052734375,
"auth_duration": 1.9073486328125e-6,
"req_duration": 0.004480123519897461,
"debug_duration": []
}
this is my API Service
ApiMain.kt
class ApiMain : Application(){
private var BASE_URL = "your url in here"
private val client = OkHttpClient().newBuilder()
.addInterceptor(HttpLoggingInterceptor().apply {
level = if (BuildConfig.DEBUG) HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE
})
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.build()
private val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build()
val services: ApiServices = retrofit.create(ApiServices::class.java)
}
and this is my API service
ApiServices.kt
interface ApiServices {
#GET("yourlink")
fun MerkKendaraan():Call<MerkKendaraan>
}
and this is my model
kendaraan.kt
data class MerkKendaraan(
#SerializedName("success")
#Expose
var success: Int? = null,
#SerializedName("dataset")
#Expose
var dataset: List<MerkMobil>? = null,
#SerializedName("sql_duration")
#Expose
var sql_duration: String? = null,
#SerializedName("auth_duration")
#Expose
var auth_duration: String? = null,
#SerializedName("req_duration")
#Expose
var req_duration: String? = null
)
data class MerkMobil(
#SerializedName("id")
#Expose
var id: String? = null,
#SerializedName("nama")
#Expose
var nama: String? = null
)
and this is my main activity
AddKendaraan
class AddKendaraan : AppCompatActivity() {
private var merk : ArrayList<MerkMobil> = ArrayList()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_add_kendaraan)
configspinnermobil()
}
private fun configspinnermobil() {
val spinner: Spinner = findViewById(R.id.spinnerMerk)
ApiMain().services.MerkKendaraan().enqueue(object :
Callback<MerkKendaraan> {
override fun onResponse(call: Call<MerkKendaraan>, response: Response<MerkKendaraan>) {
//Tulis code jika response sukses
Log.d("data api", "data masuk")
if(response.code() == 200){
merk = response.body()?.dataset as ArrayList<MerkMobil>
var data : MutableList<String> = ArrayList()
merk.forEach {
data.add(0,it.nama.toString())
}
spinner.adapter = ArrayAdapter<String>(this#AddKendaraan, R.layout.support_simple_spinner_dropdown_item, data)
}else{
}
}
override fun onFailure(call: Call<MerkKendaraan>, t: Throwable){
//Tulis code jika response fail
Toast.makeText(applicationContext, t.message, Toast.LENGTH_LONG).show()
Log.d("data api", "data tidak masuk")
}
})
}