File tree Expand file tree Collapse file tree
java/com/example/techexactly/model Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22<manifest xmlns : android =" http://schemas.android.com/apk/res/android"
33 xmlns : tools =" http://schemas.android.com/tools" >
44
5+ <uses-permission android : name =" android.permission.INTERNET" />
6+
57 <application
68 android : allowBackup =" true"
79 android : dataExtractionRules =" @xml/data_extraction_rules"
Original file line number Diff line number Diff line change 1+ package com.example.techexactly.model.dataclass
2+
3+ data class User (
4+ val id : Long ,
5+ val name : String ,
6+ val username : String ,
7+ val email : String ,
8+ val address : Address ,
9+ val phone : String ,
10+ val website : String ,
11+ val company : Company ,
12+ )
13+
14+ data class Address (
15+ val street : String ,
16+ val suite : String ,
17+ val city : String ,
18+ val zipcode : String ,
19+ val geo : Geo ,
20+ )
21+
22+ data class Geo (
23+ val lat : String ,
24+ val lng : String ,
25+ )
26+
27+ data class Company (
28+ val name : String ,
29+ val catchPhrase : String ,
30+ val bs : String ,
31+ )
Original file line number Diff line number Diff line change 1+ package com.example.techexactly.model.network
2+
3+ import com.google.gson.GsonBuilder
4+ import okhttp3.OkHttpClient
5+ import okhttp3.logging.HttpLoggingInterceptor
6+ import retrofit2.Retrofit
7+ import retrofit2.converter.gson.GsonConverterFactory
8+
9+ private const val BASE_URL = " https://jsonplaceholder.typicode.com/"
10+
11+ fun provideRetrofit (): Retrofit {
12+ val loggingInterceptor = HttpLoggingInterceptor ().apply {
13+ level = HttpLoggingInterceptor .Level .BODY
14+ }
15+ val okHttpClient = OkHttpClient .Builder ().addInterceptor(loggingInterceptor).build()
16+ val gson = GsonBuilder ().setLenient().create()
17+
18+ return Retrofit .Builder ()
19+ .baseUrl(BASE_URL )
20+ .client(okHttpClient)
21+ .addConverterFactory(GsonConverterFactory .create(gson))
22+ .build()
23+ }
24+
25+ fun provideUserApi (retrofit : Retrofit ): UserApi {
26+ return retrofit.create(UserApi ::class .java)
27+ }
Original file line number Diff line number Diff line change 1+ package com.example.techexactly.model.network
2+
3+ import com.example.techexactly.model.dataclass.User
4+ import retrofit2.Response
5+ import retrofit2.http.GET
6+
7+ interface UserApi {
8+ @GET(" /users" )
9+ suspend fun getUsers (): Response <List <User >>
10+ }
Original file line number Diff line number Diff line change 1+ package com.example.techexactly.model.repository
2+
3+ import com.example.techexactly.model.dataclass.User
4+ import com.example.techexactly.model.network.UserApi
5+ import kotlinx.coroutines.Dispatchers
6+ import kotlinx.coroutines.flow.Flow
7+ import kotlinx.coroutines.flow.flow
8+ import kotlinx.coroutines.flow.flowOn
9+
10+ class UserRepository (private val userApi : UserApi ) {
11+ fun getUsers (): Flow <Result <List <User >>> = flow {
12+ try {
13+ // Switch to Dispatchers.IO for the network call.
14+ val response = userApi.getUsers()
15+ if (response.isSuccessful) {
16+ // Handle success case.
17+ val users = response.body() ? : emptyList()
18+ emit(Result .success(users))
19+ } else {
20+ // Handle HTTP error cases.
21+ emit(Result .failure(Exception (" Error: ${response.code()} ${response.message()} " )))
22+ }
23+ } catch (e: Exception ) {
24+ // Handle any exception during the network call.
25+ emit(Result .failure(Exception (" Error fetching users: ${e.message} " )))
26+ }
27+ }.flowOn(Dispatchers .IO ) // Use flowOn to change the context of the flow to Dispatchers.IO
28+ }
You can’t perform that action at this time.
0 commit comments