๐Ÿ”— Retrofit Library in Android – เคชूเคฐी เคœाเคจเค•ाเคฐी เคนिंเคฆी เคฎें

        ๐Ÿ”— Retrofit Library in Android – เคชूเคฐी เคœाเคจเค•ाเคฐी เคนिंเคฆी เคฎें


๐Ÿš€ Retrofit เค•्เคฏा เคนै?

Retrofit เคเค• type-safe HTTP client เคนै เคœिเคธे Square เค•ंเคชเคจी เคจे develop เค•िเคฏा เคนै। เคฏเคน Android เค”เคฐ Java เคฎें API calls เค•ो manage เค•เคฐเคจे เค•े เคฒिเค เคธเคฌเคธे popular เค”เคฐ เค†เคธाเคจ library เคนै।

Retrofit เค†เคชเค•ो RESTful APIs เคธे data เคฒेเคจे เค”เคฐ เคญेเคœเคจे เค•ी เคธुเคตिเคงा เคฆेเคคा เคนै เค”เคฐ เคฏเคน JSON/XML response เค•ो directly Java/Kotlin object เคฎें convert เค•เคฐ เคฆेเคคा เคนै।


๐Ÿ“ฆ Retrofit เค•्เคฏों เค‡เคธ्เคคेเคฎाเคฒ เค•เคฐें?

  • ✅ API calls เค•ो manage เค•เคฐเคจा เค†เคธाเคจ

  • ✅ JSON parsing automatic (Gson converter เคธे)

  • ✅ Clean เค”เคฐ readable code

  • ✅ Background thread เคชเคฐ เค•ाเคฎ เค•เคฐเคคा เคนै (Asynchronous support)

  • ✅ Error handling built-in เคนोเคคा เคนै


⚙️ Retrofit เค•ैเคธे เค•ाเคฎ เค•เคฐเคคा เคนै?

Retrofit 3 เคšीเคœ़ों เคชเคฐ เค†เคงाเคฐिเคค เคนोเคคा เคนै:

  1. Base URL – API เค•ा root path

  2. Interface – เคœिเคธเคฎें endpoints define เค•िเค เคœाเคคे เคนैं

  3. Model Class – JSON เค•ो Java/Kotlin object เคฎें map เค•เคฐเคจे เค•े เคฒिเค


๐Ÿ”ง Retrofit Setup – Step by Step

Step 1: Gradle Dependency เคœोเคก़ें


implementation 'com.squareup.retrofit2:retrofit:2.11.0' implementation 'com.squareup.retrofit2:converter-gson:2.11.0'

Step 2: Model Class เคฌเคจाเคँ

เคฎाเคจ เคฒीเคœिเค เคนเคฎ เคเค• user API เคธे เคกेเคŸा เคฒा เคฐเคนे เคนैं:

json
{ "name": "Rehan", "email": "rehan@gmail.com" }

เคคो Java model class เคนोเค—ी:


public class User { private String name; private String email; public String getName() { return name; } public String getEmail() { return email; } }

Step 3: API Interface เคฌเคจाเคँ


import retrofit2.Call; import retrofit2.http.GET; public interface ApiService { @GET("users") Call<List<User>> getUsers(); }

Step 4: Retrofit Instance เคฌเคจाเคँ


Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://yourapi.com/api/") .addConverterFactory(GsonConverterFactory.create()) .build(); ApiService apiService = retrofit.create(ApiService.class);

Step 5: API Call เค•เคฐें


Call<List<User>> call = apiService.getUsers(); call.enqueue(new Callback<List<User>>() { @Override public void onResponse(Call<List<User>> call, Response<List<User>> response) { if (response.isSuccessful()) { List<User> users = response.body(); // เคฏเคนाँ users list เค•ो use เค•เคฐें } } @Override public void onFailure(Call<List<User>> call, Throwable t) { t.printStackTrace(); } });

⚠️ Retrofit เค•े เคธाเคฅ Common Concepts

TermDescription
@GET, @POST, @PUT, @DELETEHTTP Methods
@PathURL เค•े dynamic เคนिเคธ्เคธों เค•े เคฒिเค
@QueryURL query parameters
@BodyPOST request เค•े เคฒिเค body
Call<T>API เคธे response handle เค•เคฐเคจे เค•े เคฒिเค

๐Ÿ›ก️ Retrofit เค•ा Use Case เค•เคนां-เค•เคนां เคนोเคคा เคนै?

  • Login/Signup API

  • Product Listing

  • Firebase Cloud Functions access

  • JSON-based REST APIs


๐Ÿง  เคจिเคท्เค•เคฐ्เคท (Conclusion)

Retrofit Android เคฎें API communication เค•ो เคฌेเคนเคฆ เค†เคธाเคจ เค”เคฐ structured เคฌเคจा เคฆेเคคा เคนै। เคฏเคน beginners เค”เคฐ professionals เคฆोเคจों เค•े เคฒिเค best solution เคนै เค…เค—เคฐ เค†เคช clean เค”เคฐ maintainable code เคšाเคนเคคे เคนैं।

✅ Retrofit เค•ा เค‡เคธ्เคคेเคฎाเคฒ เค•เคฐเค•े เค†เคช เคœเคฒ्เคฆी เค”เคฐ เคธुเคฐเค•्เคทिเคค เคคเคฐीเค•े เคธे APIs เคธे connect เค•เคฐ เคธเค•เคคे เคนैं।

Comments

Popular posts from this blog

๐Ÿ“˜ Top 500 Java Interview Questions (With Topics)

Git And GitHub Collaborators and teams

Android Interview Question and Answer