Я проверил его в Postman и уверен, что его ответ представляет собой массив объектов, но когда я попытался его использовать, я столкнулся с ожидаемым BEGIN_OBJECT, но был Ошибка BEGIN_ARRAY, поэтому я изменил тип ответа API на «Список», чтобы посмотреть, что произойдет, и понял, что вместо «Массива объектов» он возвращает массивы массивов объектов, и во всех из них объекты имеют значение NULL и не имеют допустимого ответа.
Код: Выделить всё
fun createApiService(): ApiService {
val client = OkHttpClient.Builder()
.addInterceptor(
BasicAuthInterceptor(
"username",
"password"
)
)
.build()
val api = Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build()
return api.create(ApiService::class.java)
}
class BasicAuthInterceptor(username: String, password: String) : Interceptor {
private var credentials: String = Credentials.basic(username, password)
override fun intercept(chain: Interceptor.Chain): okhttp3.Response {
var request = chain.request()
request = request.newBuilder().header("Authorization", credentials).build()
return chain.proceed(request)
}
}
авторизации:
Код: Выделить всё
data class ProductResponse(
val products: List
)
data class Product(
/// its attributes are here
)
Код: Выделить всё
interface ApiService {
@GET("products")
suspend fun getAllProducts(): List
}
Код: Выделить всё
class ProductRepositoryImpl(
private val apiService: ApiService,
) : ProductRepository {
override suspend fun getProducts(isInternetConnected: Boolean): List {
val dataFromServer = apiService.getAllProducts()
Log.v("api",dataFromServer.toString())
return dataFromServer[0].products
}
}
[ProductResponse(products=null), ProductResponse(products=null), ProductResponse( Products = null), ProductResponse (products = null), ProductResponse (products = null), ProductResponse (products = null), ProductResponse (products = null), ProductResponse (products = null), ProductResponse (products = null), ProductResponse (products) =null)]
Мне не удалось найти конкретную причину этой проблемы.
Подробнее здесь: https://stackoverflow.com/questions/784 ... trofit-and
Мобильная версия