10

In my code, I want to send post request with basic auth.

Here is my postman screenshot :

enter image description here

here is my apiInterface class

@FormUrlEncoded
    @POST("GetBarcodeDetail")
    Call<PreliminaryGoodsAcceptResponse> PRELIMINARY_GOODS_ACCEPT_RESPONSE_CALL(@Field("ProcName") String procName, @Field("Barcode") String barcode, @Field("LangCode") String langCode);

here is my apiclient

public class ApiClient {

    public static final String BASE_URL = "http://192.**********";
    private static Retrofit retrofit = null;
    private static OkHttpClient sClient;

    public static Retrofit getClient() {
        if(sClient == null) {
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            sClient = new OkHttpClient.Builder()
                    .addInterceptor(new HttpLoggingInterceptor(HttpLoggingInterceptor.Logger.DEFAULT))
                    .addInterceptor(interceptor)
                    .build();
        }

        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(sClient)
                    .build();
        }
        return retrofit;
    }

}

My question is how can i send post request,using header :

Header Username : EBA Token :
34242353453456563DSFS

Levi Moreira
  • 11,917
  • 4
  • 32
  • 46
cartoonworld
  • 133
  • 1
  • 1
  • 8

4 Answers4

22

This is so far the easiest method i have ever tried for "Basic Authentication".

Use the below code to generate the auth header (API/Repository class)

 var basic = Credentials.basic("YOUR_USERNAME", "YOUR_PASSWORD")

Pass this as header to the webservice call (API/Repository class)

 var retrofitCall = myWebservice.getNewsFeed(basic)

Add the basic header as parameter (Retrofit Webservice interface class)

 @GET("newsfeed/daily")
 fun getNewsFeed(@Header("Authorization") h1:String):Call<NewsFeedResponse>

Sorry, my code is in Kotlin, but can be easily translated to Java.

References: https://mobikul.com/basic-authentication-retrofit-android/

Ajith M A
  • 3,838
  • 3
  • 32
  • 55
5

make header like this way..

 private Retrofit getClient(final Context context) {

    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient.Builder client = new OkHttpClient.Builder();
    client.readTimeout(60, TimeUnit.SECONDS);
    client.writeTimeout(60, TimeUnit.SECONDS);
    client.connectTimeout(60, TimeUnit.SECONDS);
    client.addInterceptor(interceptor);
    client.addInterceptor(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            if (context == null) {
                request = request
                        .newBuilder()
                        .build();
            } else {
                request = request
                        .newBuilder()
                        .addHeader("Authorization", "Bearer " + AppSetting.getStringSharedPref(context, Constants.USER_KEY_TOKEN, ""))
                        .build();
            }
            return chain.proceed(request);
        }
    });

    retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .client(client.build())
            .addConverterFactory(GsonConverterFactory.create())
            .build();


    return retrofit;
}
3

Use Header annotation

@FormUrlEncoded
    @POST("GetBarcodeDetail")
    Call<PreliminaryGoodsAcceptResponse> PRELIMINARY_GOODS_ACCEPT_RESPONSE_CALL(@Header("Authorization") token: String,@Field("ProcName") String procName, @Field("Barcode") String barcode, @Field("LangCode") String langCode);
Mirza Ahmed Baig
  • 5,605
  • 3
  • 22
  • 39
0

Simple-Retrofit-API-request-and-Data-Loading Here I just add the project where create the API call to access data from database using retrofit library; which is leading library to access data on network. And display the accessed data in the List format. Create the Simple Android Studio Project with Empty Activity. Create the Adapter and activity item to show normal lists in android app. Now Create the App class extending Application, as Application class is a singleton that you can access from any activity or anywhere else you have a Context object. You can check the more details about Application class from https://github.com/codepath/android_guides/wiki/Understanding-the-Android-Application-Class Why extend an Application class? https://developer.android.com/reference/android/app/Application.html

Add android:name=".YourApplication" i.e. class name extending the Application class in android. and class will be like public class YourApplication extends Application Init the Retrofit in Application class

//network code start 
//init http logger  
httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
// init client  client = new OkHttpClient.Builder()  
.addInterceptor(httpLoggingInterceptor)
 .addInterceptor(new Interceptor() {
  @Override public Response intercept(Chain chain) throws IOException {
   Request request = chain.request();
   Request request2 = request.newBuilder().build();
   return chain.proceed(request2);
  }
 }).connectTimeout(30, TimeUnit.SECONDS).writeTimeout(30, TimeUnit.SECONDS).readTimeout(30, TimeUnit.SECONDS).build();
Gson gson = new GsonBuilder().setLenient().create();
Retrofit mRetrofit = new Retrofit.Builder().baseUrl(Constants.API_BASE_URL).client(client).addConverterFactory(GsonConverterFactory.create(gson)).build();
mWebservice = mRetrofit.create(Webservice.class);

While Constants.API_BASE_URL is base url Create the Webervice.class where you can call the API with parameters e.g. In case of GET Method:

@GET("webservices/GetAllClientsDemoRetro.php") 
Call updateChatStatus(); 

In case of POST method:

@FormUrlEncoded  
@Headers({"Content-Type: application/x-www-form-urlencoded"})  
@POST("webservices/GetAllClientsDemoRetro.php")  

Call updateChatStatus();
You can See the more in details About Retrofit on Official API declaration here: http://square.github.io/retrofit/
We can parse the values with POJO i.e. Setter and Getter, using the Parceble class. Since parsing key name should be equal to the value we are receiving from the JSON response. POJO class should be declared like public class ClientData implements Parcelable { then declare the keys in the class, key values means

public class ClientData implements Parcelable 
{  
public String client_id;  
public String company_name;  
public String address_line;  
public String city;  
public String pincode;  
public String state;  
public String country;  
}

Now using Alt+Enter i.e. select the option Add Parceble Implementation and press enter. Then automatically parceble class will be added. Also you have to add Setter and Getter method in class using Alt + Insert. Note: Don’t add the Setter and Getter methods for CREATER: Creater<> method If you want to use different key that JSON response key, then you should use Serialization. When I was using same key then its is like public String client_id; But when I am using the Serialization, then I can use like @Serializattion(“client_id”) public String ClientID; Now last but not a list, We call the API using retrofit, and use the response to view the Item in list-

RetroFitApplication.getWebservice().updateChatStatus().enqueue(new Callback() {
 @Override public void onResponse(Call call, Response response) {
  Log.d("retrofilt success", "" + response.body());
  if (response.body() != null) {
   clientResponceData = response.body();
   Gson gson = new Gson();
   String body = gson.toJson(response.body());
   Log.d("retrofilt success2", "clientData" + clientResponceData.getResponse());
   if (clientResponceData.getResponse() != null) {
    initRV();
   }
  } else {
   // Empty Client List  Toast.makeText(ClientList.this, "Empty List", Toast.LENGTH_SHORT).show();  
  }
 }

 @Override public void onFailure(Call call, Throwable t) {
  Log.d("retrofilt error", "" + t);
  Toast.makeText(ClientList.this, "No Internet Connection", Toast.LENGTH_SHORT).show();
 }
});

By using the Construction in Adapter, we can use the values from the response. Guys I added this repository to get the Entire idea of calling the API and get the response from server using the Retrofit Library. I write this entire documents in details with simple word.

tejraj
  • 300
  • 2
  • 4
  • 16