0

I am posting Events to Google Calendar using their REST API from Java (GAE), NOT the client library.

This works very well, except that non-ASCII characters are showing up as question marks.

Things I have done to try to address this (some are clutching at straws):

  • The content-type is set to UTF-8: "Content-Type", "application/json; charset=UTF-8"
  • I have tried URL encoding the "summary" (which is the Event title) field within the JSON body
  • I tried html escaping the "summary" field within the JSON body
  • Searched StackOverflow, and I found this reference that seems like the same problem, but it's in the context of PHP, not Java, so (I think) String handling is quite different, and therefore it doesn't seem to directly apply. Basically this one says make sure that the summary value itself is UTF-8 encoded, and uses a utf8_encode function for which there isn't a direct Java comparison

I realize that one answer is probably just to use the Java library, but for various reasons I don't want to do that unless I absolutely have to.

Any ideas please? Thank you.

EDIT TO ADD CODE:

I create a Scribe Request like this (and, yes, I know that this is not what Scribe is intended for...):

String url = String.format("https://www.googleapis.com/calendar/v3/calendars/%s/events", inTargetCalendar.getId());
OAuthRequest req = new OAuthRequest(Verb.POST, url);
req.addQuerystringParameter("access_token", <TOKEN>);
JSONObject jBody = new JSONObject();
jBody.put( "start", <START> );
jBody.put( "end", <END> );
jBody.put( "summary", getSummary() );
log.info("*** getSummary() is: " + getSummary());
jBody.put( "colorId", getColorId() );
req.addPayload(jBody.toString());
req.addHeader("Content-Type", "application/json; charset=UTF-8");

Note that in this example, when I look at my log file, the logs show (correctly):

*** getSummary() is: Côte Brasserie

I then post the request like this:

    Response response=null;
    try {
        response = request.send();
    } catch (...) {...}

Per notes above, this works perfectly, except that the "ô" doesn't show up correctly. Specifically when I look at the Event through the Google calendar website it looks like this:

C?te Brasserie

I.e. the "ô" shows up as a "?". This is the same for other non-ASCII characters (from what I have seen).

Community
  • 1
  • 1
nickpharris
  • 385
  • 5
  • 17
  • Please attach your code first. – luc Aug 23 '14 at 09:12
  • @luc - done. Thanks for looking at it... – nickpharris Aug 24 '14 at 01:58
  • Cool, the implementation of getSummary would be nice too. Have you tried adding Charset.forName("UTF-8").encode(getSummary()) in your jBody.put? – luc Aug 24 '14 at 07:16
  • Thanks @luc. getSummary() just exposes an instance variable containing the event name. That data comes from another webservice. I am thinking that because when I log that value into my log file it shows up correctly, that the data must be represented/held correctly within the String - but perhaps that's not right? Anyway, I will try your recommendation re the .encode and confirm - thanks again. – nickpharris Aug 24 '14 at 15:53
  • @luc - unfortunately Charset.forName("UTF-8").encode(getSummary()) doesn't work... gives me a title of the Event "java.nio.HeapByteBuffer[pos=0 lim=15 cap=15]" in Google Calendar... Any other ideas please? – nickpharris Aug 27 '14 at 05:28

1 Answers1

0

The answer was: give up trying to do this without the SDK even though that's what I wanted, and just use the SDK... and it worked straight away - non-ASCII characters showing up fine now.

nickpharris
  • 385
  • 5
  • 17