I have an app that is to register people into a platform but I get a response of Unauthenticated
each time I submit the form data. The form is submitted using an API which requires a bearer token for each post request with the aid of retrofit. I have been out of touch with Java.
Note: its just a plain form. No authentication has been implemented in the app.
My ApiClient.java class
public class ApiClient { private static Retrofit getRetrofit(){ HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(); httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor).build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("xxxxxxxxxxxxx") .addConverterFactory(GsonConverterFactory.create()) .client(okHttpClient) .build(); return retrofit; } public static UserService getUserService(){ UserService userService = getRetrofit().create(UserService.class); return userService; } }
My UserService.java class
public interface UserService { @POST("algonapi/api/enroll_vehicle") Call<UserResponse> saveUser(@Body UserRequest userRequest); }
My saveUser Method
public void saveUser(UserRequest userRequest){ Call<UserResponse> userResponseCall = ApiClient.getUserService().saveUser(userRequest); userResponseCall.enqueue(new Callback<UserResponse>() { @Override public void onResponse(Call<UserResponse> call, Response<UserResponse> response) { if (response.isSuccessful()){ Toast.makeText(MainActivity.this, "Registration Successfull! Click on Reset Form to Start a New Enumeration...", Toast.LENGTH_LONG).show(); }else { Toast.makeText(MainActivity.this, "Registration Failed!", Toast.LENGTH_LONG).show(); } } @Override public void onFailure(Call<UserResponse> call, Throwable t) { Toast.makeText(MainActivity.this, "Registration Failed!" +t.getLocalizedMessage(), Toast.LENGTH_LONG).show(); } }); }
My UserRequest
package com.example.xxxxx; public class UserRequest { private String FullName; private String StickerNumber; private String Address; private String Email; private String Phone; private String Nationality; private String State; private String LGA; private String RC; private String DriversLicenseNo; private String LicenseIssued; private String LicenseExpiry; private String VehicleType; private String VehicleLicense; private String VehicleTyres; private String LGAofOperation; private String NOKFullName; private String NOKAddress; private String NOKPhone; private String NOKEmail; private String NOKNationality; private String NOKState; public String getFullName() { return FullName; } public void setFullName(String fullName) { FullName = fullName; } public String getStickerNumber() { return StickerNumber; } public void setStickerNumber(String stickerNumber) { StickerNumber = stickerNumber; } public String getAddress() { return Address; } public void setAddress(String address) { Address = address; } public String getEmail() { return Email; } public void setEmail(String email) { Email = email; } public String getPhone() { return Phone; } public void setPhone(String phone) { Phone = phone; } public String getNationality() { return Nationality; } public void setNationality(String nationality) { Nationality = nationality; } public String getState() { return State; } public void setState(String state) { State = state; } public String getLGA() { return LGA; } public void setLGA(String LGA) { this.LGA = LGA; } public String getRC() { return RC; } public void setRC(String RC) { this.RC = RC; } public String getDriversLicenseNo() { return DriversLicenseNo; } public void setDriversLicenseNo(String driversLicenseNo) { DriversLicenseNo = driversLicenseNo; } public String getLicenseIssued() { return LicenseIssued; } public void setLicenseIssued(String licenseIssued) { LicenseIssued = licenseIssued; } public String getLicenseExpiry() { return LicenseExpiry; } public void setLicenseExpiry(String licenseExpiry) { LicenseExpiry = licenseExpiry; } public String getVehicleType() { return VehicleType; } public void setVehicleType(String vehicleType) { VehicleType = vehicleType; } public String getVehicleLicense() { return VehicleLicense; } public void setVehicleLicense(String vehicleLicense) { VehicleLicense = vehicleLicense; } public String getVehicleTyres() { return VehicleTyres; } public void setVehicleTyres(String vehicleTyres) { VehicleTyres = vehicleTyres; } public String getLGAofOperation() { return LGAofOperation; } public void setLGAofOperation(String LGAofOperation) { this.LGAofOperation = LGAofOperation; } public String getNOKFullName() { return NOKFullName; } public void setNOKFullName(String NOKFullName) { this.NOKFullName = NOKFullName; } public String getNOKAddress() { return NOKAddress; } public void setNOKAddress(String NOKAddress) { this.NOKAddress = NOKAddress; } public String getNOKPhone() { return NOKPhone; } public void setNOKPhone(String NOKPhone) { this.NOKPhone = NOKPhone; } public String getNOKEmail() { return NOKEmail; } public void setNOKEmail(String NOKEmail) { this.NOKEmail = NOKEmail; } public String getNOKNationality() { return NOKNationality; } public void setNOKNationality(String NOKNationality) { this.NOKNationality = NOKNationality; } public String getNOKState() { return NOKState; } public void setNOKState(String NOKState) { this.NOKState = NOKState; } }
Advertisement
Answer
If you most of your https requests need authentication then the first answer is perfect but if some of your requests need then you can pass the header to each methods.
public interface UserService { @POST("algonapi/api/enroll_vehicle") Call<UserResponse> saveUser( @Header("Authorization") String token, @Body UserRequest userRequest ); }
While calling the method simply pass your token along with userRequest.