I am creating an app that consumes data from a server with Retrofit2. Get i Post queries I have created them and they work perfectly for me.
@GET("baseclientes.php")
Call<ArrayList<NClientes>> NClientes();
The problem comes when I want to validate that the user that is consuming the data has permissions. The idea is that the user save name and password and the server with php checks if it has permissions.
I have been looking for examples and tutorials and in the end I have seen that one solution is to use an interceptor. I modified my API to add the interceptor but I do not know how to work the data that arrives at the server with php. The truth is that I am very new to the use of Retrofit.
static OkHttpClient okHttpClient = new OkHttpClient().newBuilder().addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Interceptor.Chain chain) throws IOException {
Request originalRequest = chain.request();
Request.Builder builder = originalRequest.newBuilder().header("Authorization",
Credentials.basic("Usuario", "contraseña"));
Request newRequest = builder.build();
return chain.proceed(newRequest);
}
}).build();
public static final String BASE_URL = "http://www.miweb.com/";
private static Retrofit retrofit = null;
public static Retrofit getApi(){
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
}
return retrofit;
}
With debug I see that the header is created. But when creating the php file I do not know how to receive the header data. I've tried this but it's not ...
$header = "Authorization: Basic " . base64_decode($username . ':' . $password);
Any ideas? Thanks!