将字节数组转换为Base64会创建空的,中断URL

时间:2015-08-02 20:32:14

标签: android ruby-on-rails base64 android-volley

在android中我有一个参数,它是转换为字节数组的天数列表,然后将Base64传递给URL中的web服务:

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";

但是,在执行GET请求时,它会出错。然后,检查日志,似乎在转换后的字符串中添加了一个非常大的空间,从而破坏了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

我已经在浏览器中测试了这个空间的删除,一切都可以正常使用Web服务。

如何在不破坏Base64转换有效性的情况下删除此空间?

1 个答案:

答案 0 :(得分:0)

我认为你应该使用URL_SAFE模式而不是&#39; DEFAULT`模式来编码你的字节数组。 根据{{​​3}}:

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 /.