Dear Friends...

I managed to make an implementation with Retrofit using basic 
authentication, ie, sending the username and password, and can authenticate 
to the webservice.
However, I can not capture the token that is returned in json.

I follow this tutorial
https://futurestud.io/tutorials/android-basic-authentication-with-retrofit


Below is the code that implemented.

ServiceGenerator.java

public class ServiceGenerator {

    public static final String API_BASE_URL = "https://NOME_APP.herokuapp.com/ 
<https://nome_app.herokuapp.com/>";

    private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

    private static Retrofit.Builder builder =
            new Retrofit.Builder()
            .baseUrl(API_BASE_URL)
            .addConverterFactory(GsonConverterFactory.create());

    public static <S> S createService(Class<S> serviceClass){
        return createService(serviceClass, null, null   );
    }


    public static <S> S createService(Class<S> serviceClass, String name, 
String password){
        if(name != null && password != null){
            String credentials = name + ":" + password;

            final String basic = "Basic " + 
Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);

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

                    Request.Builder requestBuilder = original.newBuilder()
                            .header("Authorization", basic)
                            .header("Accept", "application/json")
                            .method(original.method(), original.body());

                    Request request = requestBuilder.build();
                    return chain.proceed(request);
                }
            });
        }

        OkHttpClient client = httpClient.build();
        Retrofit retrofit = builder.client(client).build();
        return retrofit.create(serviceClass);
    }
}



LoginService.java

public interface LoginService {

    @POST("api/autenticar")
    Call<Login> basicLogin();
}



Login.java

public class Login {
    @SerializedName("name")
    private String name;

    @SerializedName("password")
    private String password;

    @SerializedName("token")
    private String token;

    public Login(String name, String password){
        this.name = name;
        this.password = password;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }
}




MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    try {
        logarWebService();
    } catch (IOException e) {
        e.printStackTrace();
    }

.
.
.

private void logarWebService() throws IOException {

    LoginService loginService  = 
ServiceGenerator.createService(LoginService.class, "appLoginWS", "appSenhaWS");
    Call<Login> call = loginService.basicLogin();

    call.enqueue(new Callback<Login>() {
        @Override
        public void onResponse(Call<Login> call, Response<Login> response) {
            if(response.isSuccessful()){
                Log.d(TAG, "responseOK");
            }else{
                Log.d(TAG, "responseNULL");
            }
        }

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

        }
    });

    Login login = call.execute().body();

}



-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
To post to this group, send email to android-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/a1c51cfe-a897-4cb3-b2c5-87b19df6597f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to