Retrofit
Understanding Retrofit and its implementation in Android
Introduction to Retrofit
Retrofit Android में network operations को handle करने के लिए एक type-safe HTTP client है। यह REST APIs के साथ communicate करने का एक simple और efficient way provide करता है।
Retrofit is a type-safe HTTP client for handling network operations in Android. It provides a simple and efficient way to communicate with REST APIs.
Key Features (मुख्य विशेषताएं):
- Retrofit type-safe API calls provide करता है
- Retrofit provides type-safe API calls
- Retrofit multiple data formats को support करता है
- Retrofit supports multiple data formats
- Retrofit coroutines और RxJava को support करता है
- Retrofit supports coroutines and RxJava
- Retrofit request/response interceptors provide करता है
- Retrofit provides request/response interceptors
Retrofit Components (रेट्रोफिट कंपोनेंट्स)
Retrofit के main components:
Main components of Retrofit:
Component (कंपोनेंट) | Description (विवरण) |
---|---|
API Interface | API endpoints को define करता है |
API Interface | Defines API endpoints |
Converter | Data को convert करता है (JSON, XML, etc.) |
Converter | Converts data (JSON, XML, etc.) |
Interceptor | Request/response को modify करता है |
Interceptor | Modifies request/response |
Implementation Example (इम्प्लीमेंटेशन उदाहरण)
Retrofit को implement करने का example:
Example of implementing Retrofit:
// Data class
data class User(
val id: Int,
val name: String,
val email: String
)
// API Interface
interface ApiService {
@GET("users/{id}")
suspend fun getUser(@Path("id") userId: Int): User
@POST("users")
suspend fun createUser(@Body user: User): User
@GET("users")
suspend fun getUsers(): List
}
// Retrofit setup
object RetrofitClient {
private const val BASE_URL = "https://api.example.com/"
private val okHttpClient = OkHttpClient.Builder()
.addInterceptor(HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
})
.build()
private val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build()
val apiService: ApiService = retrofit.create(ApiService::class.java)
}
// Repository implementation
class UserRepository {
private val apiService = RetrofitClient.apiService
suspend fun getUser(userId: Int): User {
return apiService.getUser(userId)
}
suspend fun createUser(user: User): User {
return apiService.createUser(user)
}
suspend fun getUsers(): List {
return apiService.getUsers()
}
}
Error Handling (एरर हैंडलिंग)
Retrofit में error handling का example:
Example of error handling in Retrofit:
// Error handling with try-catch
suspend fun getUserWithErrorHandling(userId: Int): Result {
return try {
val user = apiService.getUser(userId)
Result.success(user)
} catch (e: HttpException) {
Result.failure(Exception("HTTP Error: ${e.code()}"))
} catch (e: IOException) {
Result.failure(Exception("Network Error: ${e.message}"))
} catch (e: Exception) {
Result.failure(Exception("Unknown Error: ${e.message}"))
}
}
// Using Result in ViewModel
class UserViewModel : ViewModel() {
private val repository = UserRepository()
private val _user = MutableLiveData>()
val user: LiveData> = _user
fun loadUser(userId: Int) {
viewModelScope.launch {
_user.value = repository.getUserWithErrorHandling(userId)
}
}
}
Best Practices (सर्वोत्तम प्रथाएं)
Retrofit के साथ काम करने के best practices:
Best practices for working with Retrofit: