Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!
Deniel Kerr
Founder. Opencart
Top Partners
Retrofit is a library which is type-safe REST client for Android. Type-safe means that the compiler will validate types while compiling, and throw an error if you try to assign the wrong type to a variable.
Before Reading this blog you have to read following blog to learn the basic knowledge of the Retrofit,
Using Retrofit library to make Http Requests
Step 1: Make an interface,
public interface ApiInterface { String MOBIKUL_HOME_PAGE_DATA = "api/homepage/{customerId}"; @GET(MOBIKUL_HOME_PAGE_DATA) Call<HomepageData> getHomePageData( @Header("Authorization") String authkey , @Path("customerId") String customerId ); } 1234567891011 public interface ApiInterface { String MOBIKUL_HOME_PAGE_DATA = "api/homepage/{customerId}"; @GET(MOBIKUL_HOME_PAGE_DATA) Call<HomepageData> getHomePageData( @Header("Authorization") String authkey , @Path("customerId") String customerId ); }
Step 2: Create the AuthToken
public static String getAuthToken() { byte[] data = new byte[0]; try { data = (ApplicationConstant.API_USER_NAME + ":" + ApplicationConstant.API_PASSWORD).getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return "Basic " + Base64.encodeToString(data, Base64.NO_WRAP); } 123456789 public static String getAuthToken() { byte[] data = new byte[0]; try { data = (ApplicationConstant.API_USER_NAME + ":" + ApplicationConstant.API_PASSWORD).getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return "Basic " + Base64.encodeToString(data, Base64.NO_WRAP); }
Step 3: Create the Retrofit Client,
public class RetrofitClient { private static Retrofit retrofit = null; public static Retrofit getClient(Context mContext) { if (retrofit == null) { retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); } return retrofit; } } 1234567891011121314 public class RetrofitClient { private static Retrofit retrofit = null; public static Retrofit getClient(Context mContext) { if (retrofit == null) { retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); } return retrofit; }}
Step 4: Make your call,
public static void getHomePage(Context mContext, Callback<HomepageData> callback) { Call<HomepageData> call = RetrofitClient.getClient(mContext).create(ApiInterface.class).getHomePageData( Helper.getAuthToken() , AppSharedPref.getCustomerId(mContext) ); call.enqueue(callback); } 1234567 public static void getHomePage(Context mContext, Callback<HomepageData> callback) { Call<HomepageData> call = RetrofitClient.getClient(mContext).create(ApiInterface.class).getHomePageData( Helper.getAuthToken() , AppSharedPref.getCustomerId(mContext) ); call.enqueue(callback); } call the Api with Basic Authorization, RetrofitCalls.getHomePage(Activity.this, callback); 1 RetrofitCalls.getHomePage(Activity.this, callback);
call the Api with Basic Authorization,
Thanks for reading this blog, You have any questions or problems comment below.
Happy Coding. Stay Super.
Your email address will not be published. Required fields are marked*
Name*
Email*
Save my name email and website in this browser for the next time I comment.
The 401 error means unauthorized.
Make sure that you are using correct credentials to generate the AuthToken.
You can take the help of the backend team of your project to look into this point.
We use cookies to personalize your experience. By continuing to visit this website you agree to our use of cookies. Learn more about privacy policy
Webkul is a truly trusted and supported IT Company to develop business ideas for e-commerce. They provide professional support after deploying the solution to production and be responsible to act for fixing the reported issues or system errors. We highly encourage to deal with Webkul for business development.
Hussein Zawia
Co-founder, 24Dokan
USA
India
Global
Name
Email
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.