1

In android I have a parameter that is the list of days that is converted into a Byte array, and then Base64 to be passed to a webservice in URL as:

try {
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  ObjectOutputStream oos = new ObjectOutputStream(bos);

  // Saving the days selected (but also allowing none)
  oos.writeObject(event.getChosenDays());
  buff = bos.toByteArray();
  encoddedDaysString = Base64.encodeToString(buff, Base64.DEFAULT);

} catch (Exception e) {
}

String CreateEvent = "http://" + Global.getFeastOnline()
  + "/createEvent" + "/"
  + "0" + "/"
  + "-1" + "/"
  + "-1" + "/"
  + event.getAlarmActive().toString() + "/"
  + "0" + "/"
  + event.getAlarmTimeString() + "/"
  + encoddedDaysString + "/"
  + "0" + "/"   // Should not pass tone to path
  + event.getVibrate().toString() + "/"


  + event.getAlarmName() + "/"
  + event.getAlarmDateString()
  + ".json";

However, upon executing a GET request, it errors out. Then, checking the logs, it appears that a very large space is added to the converted string, thus breaking the URL:

08-02 16:13:17.184  11663-11663/ D/Create Events Error? com.android.volley.NoConnectionError: java.net.ProtocolException: Unexpected status line: <!DOCTYPE html>
08-02 16:14:09.204  11663-11663/ V/Create Events Error? user_id: 2
08-02 16:14:09.204  11663-11663/ V/Create Events Error? event_active: true
08-02 16:14:09.204  11663-11663/ V/Create Events Error? event_time: 16:03
08-02 16:14:09.204  11663-11663/ V/Create Events Error? event_days: rO0ABXVyADNbTGNvbS5leGFtcGxlLmZlYXN0YXBwLk1haW5NZW51Lk15RXZlbnRzLkV2ZW50JERh
    eTuousH0g5N2ggIAAHhwAAAAAA==
08-02 16:14:09.204  11663-11663/ V/Create Events Error? event_tone: 0
08-02 16:14:09.204  11663-11663/ V/Create Events Error? event_vibrate: true
08-02 16:14:09.204  11663-11663/ V/Create Events Error? event_name: Event Clock
08-02 16:14:09.204  11663-11663/ V/Create Events Error? event_date: 2015-08-03
08-02 16:14:09.204  11663-11663/ V/Create Events Error? createEven: http://(IPADDRESS):3000/createEvent/0/-1/-1/true/0/16:03/rO0ABXVyADNbTGNvbS5leGFtcGxlLmZlYXN0YXBwLk1haW5NZW51Lk15RXZlbnRzLkV2ZW50JERh
    eTuousH0g5N2ggIAAHhwAAAAAA==
    /0/true/Event Clock/2015-08-03.json

I have tested in browser the removal of this space, and everything works fine with the web-service.

How can this space be removed without destroying the validity of the Base64 conversion?

Vasseurth
  • 6,354
  • 12
  • 53
  • 81
Sauron
  • 6,399
  • 14
  • 71
  • 136

1 Answers1

0

I think you should use URL_SAFE mode instead of 'DEFAULT` mode for encoding your byte array. according to documentation :

URL_SAFE

Encoder/decoder flag bit to indicate using the "URL and filename safe" variant of Base64 (see RFC 3548 section 4) where - and _ are used in place of + and /. 
Mohammad Rahchamani
  • 5,002
  • 1
  • 26
  • 36
  • The addition of `URL_SAFE` did nothing to prevent the inclusion of that large gap/space. What I ended up doing is simply: `.replaceAll("\\s+","")` from http://stackoverflow.com/questions/5455794/removing-whitespace-from-strings-in-java – Sauron Aug 03 '15 at 14:33