There is an HTTP client:

 public class App extends Application { public static MessengerApi service; private static OkHttpClient client; private static Retrofit retrofit; @Override public void onCreate() { super.onCreate(); HttpLoggingInterceptor logger = new HttpLoggingInterceptor(); logger.setLevel(HttpLoggingInterceptor.Level.BODY); client = new OkHttpClient.Builder() .connectTimeout(60, TimeUnit.SECONDS) .readTimeout(30, TimeUnit.SECONDS) .addInterceptor(logger) .build(); retrofit = new Retrofit.Builder() .baseUrl(BuildConfig.BASE_URL) .client(client) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .build(); service = retrofit.create(MessengerApi.class); } } 

And this interface:

 public interface MessengerApi { @GET("users") Observable<User> users(); } 

I need to add authorization (username, password) now. How to do it? A little confused in the tutorials.

API:

enter image description here

  • Looking what api at you. authorization usually occurs through a POST request. If you show us the description of the function, it will be much easier to help you - miha_dev
  • @miha_dev added - Lucky_girl

4 answers 4

first, what is your authorization?

if basic, then use as an example

 "Basic " + Base64.encodeToString((username + ":" + password).getBytes("UTF-8"), Base64.NO_WRAP) 

and

 @GET("users") Observable<User> users(@Header("Authorization") String auth); 
  • And how to understand the use of basic authorization or not basic? This line "Basic" + Base64.encodeToString ((username + ":" + password) .getBytes ("UTF-8"), Base64.NO_WRAP) "is the String auth? Where should I add it? - Lucky_girl
  • en.wikipedia.org/wiki/Basic_access_authentication , a "" Basic "+ Base64.encodeToString ((username +": "+ password) .getBytes (" UTF-8 "), Base64.NO_WRAP)" in auth you shove. - Maxim Kuznetsov

Create an object for login / password

 public class RequestLogin { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } 

Then pass it as a parameter to the service function.

 public interface MessengerApi { @GET("login") Observable<User> login(RequestLogin login); } 

And use so

 RequestLogin login = new RequestLogin(); login.setUsername("moylogin"); login.getPassword("moy-parol"); call = messengerApi.login(login); 

    If you still use get, then something like this

     @GET("users") Observable<User> users(@Query("username") String username,@Query("password") String password); 

      Then the answer is better to wrap the class:

       class UserResponse { private List<User> user; } class User { private String username; private String name; private String email; private int properties; } 

      Retrofit function will be such

       @GET("users") Observable<UserResponse> login(@Query("username") String username, @Query("password") String password); 
      • Regarding @Query ("username") username - issues Annotations are not allowed here, Cannot resolve symbol "username" - Lucky_girl
      • Forgot type specify. Corrected the answer - miha_dev