Skip to content

Instantly share code, notes, and snippets.

data class DocumentsItem(
@field:SerializedName("createTime")
val createTime: String? = null,
@field:SerializedName("name")
val name: String? = null,
@field:SerializedName("updateTime")
val updateTime: String? = null,
data class Fields(
@field:SerializedName("surname")
val surname: Value? = null,
@field:SerializedName("name")
val name: Value? = null
)
data class Value(
@field:SerializedName("stringValue")
val stringValue: String? = null
)
class RetrofitClient {
companion object {
const val baseUrl = "https://firestore.googleapis.com/v1/projects/firestorerestapi/databases/"
fun getApi(): Retrofit {
return Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
interface ApiHelper {
@GET("(default)/documents/user")
fun getUser(): Call<UserResponse>
}
class ApiClient {
fun getUserFromApi(requestCallback: (isSuccess: Boolean, response: UserResponse?,message: String?) -> (Unit)) {
RetrofitClient.getApi()
.create(ApiHelper::class.java)
.getUser()
.enqueue(object : Callback<UserResponse> {
override fun onFailure(call: Call<UserResponse>, t: Throwable) {
requestCallback(false,null,t.localizedMessage)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity">
private val apiClient by lazy { ApiClient() }
apiClient.getUserFromApi { isSuccess, response, message ->
if (isSuccess) {
val name = response?.documents?.first()?.fields?.name
val surname = response?.documents?.first()?.fields?.surname
name?.let { _name ->
txt_name.text = _name.stringValue
}
surname?.let { _surname ->
txt_surname.text = _surname.stringValue
}
<uses-permission android:name="android.permission.INTERNET"/>