Question

I'm just starting with Google Cloud Endpoints, I built an API in Python and even got to use the generated sources in the Android Studio/Gradle environment.

I'm facing a problem that whenever the response from Cloud Endpoints includes a DateTime object, a NumberFormatException is thrown.

08-05 13:27:28.143  32116-32141/com.dgt.ds E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
        java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:299)
        at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
        at java.util.concurrent.FutureTask.run(FutureTask.java:137)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
        at java.lang.Thread.run(Thread.java:856)
        Caused by: java.lang.IllegalArgumentException:  [key created_at, field private com.google.api.client.util.DateTime com.google.api.services.dsApi.model.ServicesDResponse.createdAt]
        at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:797)
        at com.google.api.client.json.JsonParser.parse(JsonParser.java:438)
        at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:745)
        at com.google.api.client.json.JsonParser.parse(JsonParser.java:358)
        at com.google.api.client.json.JsonParser.parse(JsonParser.java:331)
        at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:87)
        at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:81)
        at com.google.api.client.http.HttpResponse.parseAs(HttpResponse.java:459)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
        at com.dgt.ds.MainActivity$1.doInBackground(MainActivity.java:70)
        at com.dgt.ds.MainActivity$1.doInBackground(MainActivity.java:58)
        at android.os.AsyncTask$2.call(AsyncTask.java:287)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
        ... 5 more
        Caused by: java.lang.NumberFormatException: Invalid date/time format: 2013-08-05T10:27:27.449810
        at com.google.api.client.util.DateTime.parseRfc3339(DateTime.java:301)
        at com.google.api.client.util.Data.parsePrimitiveValue(Data.java:435)
        at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:795)
        ... 17 more

this is the message object in Python (I'm using message_types.DateTimeField):

class DResponse(messages.Message):
    id = messages.StringField(1)
    created_at = message_types.DateTimeField(2)
    author = messages.StringField(3)
    description = messages.StringField(4, required=True)

how can I fix this ?

Was it helpful?

Solution

This is caused by the fact that protorpc.protojson.ProtoJson.encode_field is using isoformat on native datetime.datetime objects.

This uses microseconds instead of milliseconds (since the datetime module in Python uses microseconds) and as a result, this fails the Java client library parser.

For a short term fix you could do one of two things:

  • Stop using DateTimeField and just use an IntegerField, relying on your clients to send timestamps as integers
  • Monkey patch protorpc/protojson.py to reference a subclass of ProtoJson which behaves correctly. Essentially, isoformat() shouldn't be used and you should use a hybrid which only has 3 places after the decimal.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top