I’m trying to connect my Koltin project to the Nodejs backend server using Retrofit but I get a white blank screen whenever I run the mobile app
here is my Adapter code
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.AsyncListDiffer
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import tn.esprit.t1.databinding.ItemCatastropheBinding
import tn.esprit.t1.model.Catastrophe
class CatastropheAdapter: RecyclerView.Adapter<CatastropheAdapter.CatastropheViewHolder>() {
inner class CatastropheViewHolder(val binding: ItemCatastropheBinding): RecyclerView.ViewHolder(binding.root)
private val diffCallback = object : DiffUtil.ItemCallback<Catastrophe>(){
override fun areContentsTheSame(oldItem: Catastrophe, newItem: Catastrophe): Boolean {
return oldItem._id == newItem._id
}
override fun areItemsTheSame(oldItem: Catastrophe, newItem: Catastrophe): Boolean {
return oldItem==newItem
}
}
private val differ = AsyncListDiffer(this, diffCallback)
var catastrophes: List<Catastrophe>
get() = differ.currentList
set(value) { differ.submitList(value)}
override fun getItemCount()= catastrophes.size
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CatastropheViewHolder {
return CatastropheViewHolder(ItemCatastropheBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
))
}
override fun onBindViewHolder(holder: CatastropheViewHolder, position: Int) {
holder.binding.apply {
val catastrophe = catastrophes[position]
tvType.text = catastrophe.type
tvPlace.text = catastrophe.description
tvMag.text = catastrophe.magnitude.toString()
}
}
}
I want to display 3 text views on my recycler view ( Type, description, magnitude )
here is my class code
data class Catastrophe(
val _id: Id,
val titre: String,
val type: String,
val tsunami: Int,
val description: String,
val date: Date,
val radius: Double,
val magnitude: Double,
val latitudeDeCatastrophe: Double,
val longitudeDeCatastrophe: Double,
val createdAt: Date,
val updatedAt: Date
)
and this is my retrofit instance code
package tn.esprit.t1.repository
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.create
object RetrofitInstance {
val api: CatastropheApi by lazy {
Retrofit.Builder()
.baseUrl("http://192.168.1.105:9090/")
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(CatastropheApi::class.java)
}
}
and finally my main activity
package tn.esprit.t1
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.coroutines.launch
import retrofit2.HttpException
import retrofit2.http.HTTP
import retrofit2.http.Tag
import tn.esprit.t1.databinding.ActivityMainBinding
import tn.esprit.t1.repository.CatastropheAdapter
import tn.esprit.t1.repository.RetrofitInstance
import tn.esprit.t1.ui.theme.T1Theme
import java.io.IOException
const val TAG = "Main Activity"
class MainActivity : ComponentActivity() {
private lateinit var catastropheAdapter: CatastropheAdapter
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
setupRecyclerView()
lifecycleScope.launch {
binding.progressBar.isVisible = true
val response = try {
RetrofitInstance.api.getCatastophes()
}
catch (e:IOException) {
Log.e(TAG, "IO Exception, No Internet Connection")
binding.progressBar.isVisible = false
return@launch
}
catch (e: HttpException) {
Log.e(TAG, "HttpException, no INternet ")
binding.progressBar.isVisible = false
return@launch
}
if (response.isSuccessful && response.body() != null)
{
catastropheAdapter.catastrophes = response.body()!!
}
else
{
Log.e(TAG, "Response not successful" )
}
binding.progressBar.isVisible = false
}
}
private fun setupRecyclerView() = binding.rvCatastrophe.apply {
catastropheAdapter = CatastropheAdapter()
adapter = catastropheAdapter
layoutManager = LinearLayoutManager(this@MainActivity)
}
}