Here’s a full Kotlin project setup to fetch data from a website using Retrofit in an Android app.
Project Structure
app/
├── manifests/
│ ├── AndroidManifest.xml
├── java/com/example/appname/
│ ├── MainActivity.kt
│ ├── ApiService.kt
│ ├── Post.kt
│ ├── RetrofitClient.kt
├── res/
│ ├── layout/activity_main.xml
├── build.gradle (Module: app)
├── build.gradle (Project)
1. Add Dependencies in build.gradle (Module: app)
dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3'
}
2. Update AndroidManifest.xml
with Internet Permission
<uses-permission android:name="android.permission.INTERNET" />
3. Create Post.kt
(Data Model)
package com.example.appname
data class Post(
val userId: Int,
val id: Int,
val title: String,
val body: String
)
4. Create ApiService.kt
(API Interface)
package com.example.appname
import retrofit2.http.GET
interface ApiService {
@GET("posts")
suspend fun getPosts(): List<Post>
}
5. Create RetrofitClient.kt
(Retrofit Instance)
package com.example.appname
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
object RetrofitClient {
private const val BASE_URL = "https://jsonplaceholder.typicode.com/"
val instance: ApiService by lazy {
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(ApiService::class.java)
}
}
6. Update activity_main.xml
(UI Layout)
<?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"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/tvData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fetching data..."
android:textSize="16sp"
android:textColor="#000000" />
</LinearLayout>
7. Update MainActivity.kt
(Fetch and Display Data)
package com.example.appname
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.launch
class MainActivity : AppCompatActivity() {
private lateinit var tvData: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tvData = findViewById(R.id.tvData)
// Fetch data using Coroutines
lifecycleScope.launch {
try {
val posts = RetrofitClient.instance.getPosts()
tvData.text = posts.joinToString("\n\n") { it.title }
} catch (e: Exception) {
tvData.text = "Error: ${e.message}"
}
}
}
}
Features of this Code:
Uses Retrofit
for API calls
Uses Coroutines
for async operations
Displays API response on a TextView
Requires only Internet Permission
This project will fetch JSON data from https://jsonplaceholder.typicode.com/posts
and display it in the app.
Let me know if you need any modifications!