It seems that this question has been answered here . You should use the Base64.NO_WRAP flag when encoding a pair of username and password:
String encoding = Base64.encodeToString(authentication.getBytes(), Base64.NO_WRAP);
By default, Android Base64 util adds a newline to the end of the encoded string. This invalidates the HTTP headers and causes a Bad Request.
The Base64.NO_WRAP flag instructs the utility to create an encoded string without a newline, thereby preserving intact HTTP headers.
source share