Header parameters are never sent in retrofit 2

Viewed 96

In my Android app I'm trying to make a network call using retrofit 2 and I need to send some data in header. I tried sending them statically using @Headers, dynamically using @Header and @HeaderMap but none of them worked. I also tried to add header to okhttpclient interceptor but still not working. what could be the problem and how to solve it?

below is my retrofit code

public class ApiClient {

public static final String BASE_URL =
        "https://iconprojectss.com/2021/Tam/webServices/";
private static ApiClient apiClient;
private Retrofit retrofit;

private ApiClient(){

    Gson gson = new GsonBuilder()
            .create();

    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    httpClient.addInterceptor(new Interceptor() {
                                  @Override
                                  public Response intercept(Interceptor.Chain chain) throws IOException {
                                      Request original = chain.request();

                                      Request request = original.newBuilder()
                                              .header("lang", "en")
                                              .header("apiKey", "fd94f23499b954d6cea823567a606a3f")
                                              .method(original.method(), original.body())
                                              .build();

                                      return chain.proceed(request);
                                  }
                              });

            OkHttpClient client = httpClient.build();

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

public static synchronized ApiClient getApiClient(){

    if (apiClient == null){
        apiClient = new ApiClient();
    }
    return apiClient;
}

public ApiInterface getApiInterface(){

    return retrofit.create(ApiInterface.class);
}

}

and my interface

@Headers({
        "lang: en",
        "apiKey: fd94f23499b954d6cea823567a606a3f"

})
@POST("login/")
Call<ApiResponse> login(
                          @Body HashMap<String, Object> body);



@POST("login/")
Call<ApiResponse> login(@HeaderMap Map<String, String> headers,
                        @Body HashMap<String, Object> body);

@POST("login/")
Call<ApiResponse> login(@Header("lang") String lang,
                        @Header("apiKey") String apiKey
                        @Body HashMap<String, Object> body);

}

in the previous code I tried all the possible ways to add header in interface

Lastly here's the code where I make the call

public void loginRequest(String emailStr, String passwordStr, LoginCallbacks loginCallbacks) {

    Map<String, String> hed = new HashMap<>();
    hed.put("lang", "en");
    hed.put("apiKey", "fd94f23499b954d6cea823567a606a3f");
    this.loginCallbacks = loginCallbacks;

    HashMap<String, Object> map = new HashMap<>();
    map.put("email", emailStr);
    map.put("password", passwordStr);
    map.put("fcmToken", FCM_TOKEN);
    ApiClient.getApiClient().getApiInterface().login(
             map)
    .enqueue(new Callback<ApiResponse>() {
        @Override
        public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
            ApiResponse result = response.body();
            if (result != null) {
                int code = result.getCode();
                String message = result.getMsg();


                if (code == 200) {

                    loginCallbacks.onSuccess(message);

                } else {

                    loginCallbacks.onFail(message);
                }
            } else {

                loginCallbacks.onFail("zero");
            }
        }

        @Override
        public void onFailure(Call<ApiResponse> call, Throwable t) {

            loginCallbacks.onFail(t.getMessage());
        }
    });
}

note that the retrofit version I'm using is implementation 'com.squareup.retrofit2:retrofit:2.9.0'

0 Answers
Related