0% found this document useful (0 votes)
4 views3 pages

Job Api1

The document outlines an API client implementation using Retrofit to fetch data from the endpoint https://jsonplaceholder.typicode.com/. It includes the necessary Kotlin files: ApiClient.kt for setting up Retrofit, ApiService.kt for defining the API call, Post.kt for the data model, and activity_main.xml for the UI layout. The MainActivity class demonstrates how to make the API call and display the fetched post data in a TextView using View Binding.

Uploaded by

erwinr66
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Job Api1

The document outlines an API client implementation using Retrofit to fetch data from the endpoint https://jsonplaceholder.typicode.com/. It includes the necessary Kotlin files: ApiClient.kt for setting up Retrofit, ApiService.kt for defining the API call, Post.kt for the data model, and activity_main.xml for the UI layout. The MainActivity class demonstrates how to make the API call and display the fetched post data in a TextView using View Binding.

Uploaded by

erwinr66
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

API [job-1]

Deskripsi :
Menampilkan data dari endpoint https://jsonplaceholder.typicode.com/

1. Nama file : ApiClient.kt


object ApiClient {
private const val BASE_URL ="https://jsonplaceholder.typicode.com/"

val retrofit: Retrofit = Retrofit.Builder()


.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
}

2. Nama file : ApiService.kt

interface ApiService {
@GET("posts/3")
fun getPost(): Call<Post>
}

3. Nama file : Post.kt


data class Post(
val userId: Int,
val id: Int,
val title: String,
val body: String
)
4. Nama file : activity_main.xml
<?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/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Loading..."
android:textSize="18sp"
android:padding="8dp" />
</LinearLayout>

5. Nama file : MainActivity


class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

// Buat instance API Service


val apiService = ApiClient.retrofit.create(ApiService::class.java)

// Panggil API
apiService.getPost().enqueue(object : Callback<Post> {
override fun onResponse(call: Call<Post>, response: Response<Post>) {
if (response.isSuccessful) {
val post = response.body()
post?.let {
// Tampilkan data menggunakan View Binding
binding.textView.text = "Title: ${it.title}\n\nBody: ${it.body}"
}
}
}

override fun onFailure(call: Call<Post>, t: Throwable) {


binding.textView.text = "Error: ${t.message}"
}
})
}
}
OUTPUT

You might also like