matomo-org / matomo-java-tracker

Official Java implementation of the Matomo Tracking HTTP API.
https://matomo-org.github.io/matomo-java-tracker/
BSD 3-Clause "New" or "Revised" License
69 stars 52 forks source link

ClassCastException when trying to send PiwikRequest with EcommeceItem #13

Closed jahudi closed 8 years ago

jahudi commented 8 years ago

Hi,

when trying to send a PiwikRequest that contains an EcommerceItem a ClassCastException is thrown by the org.glassfish.json.JsonGeneratorImpl.write method since EcommerceItem cannot be cast to javax.json.JsonArray. See stack trace below.

java.lang.ClassCastException: org.piwik.java.tracking.EcommerceItem cannot be cast to javax.json.JsonArray
    at org.glassfish.json.JsonGeneratorImpl.write(JsonGeneratorImpl.java:275)
    at org.glassfish.json.JsonWriterImpl.writeArray(JsonWriterImpl.java:102)
    at org.glassfish.json.JsonWriterImpl.write(JsonWriterImpl.java:141)
    at org.glassfish.json.JsonArrayBuilderImpl$JsonArrayImpl.toString(JsonArrayBuilderImpl.java:266)
    at org.piwik.java.tracking.PiwikJsonArray.toString(PiwikJsonArray.java:61)
    at org.piwik.java.tracking.PiwikRequest.getUrlEncodedQueryString(PiwikRequest.java:1469)
    at org.piwik.java.tracking.PiwikTracker.sendRequest(PiwikTracker.java:49)
    ...

Test code that creates the error:

PiwikRequest request = new PiwikRequest(3, new URL("http://test-app.com/"));
request.enableEcommerce();
request.setEcommerceId(UUID.randomUUID().toString());
request.setEcommerceRevenue(5.99);
EcommerceItem item = new EcommerceItem("cashbundle.l", "Cash Bundle L", "Consumable", 5.99, 1);
request.addEcommerceItem(item);
PiwikTracker tracker = new PiwikTracker("http://my-tracking-server.com/piwik.php");
HttpResponse response = tracker.sendRequest(request);

The piwik-java-tracker library and its dependencies are included using maven:

<dependency>
    <groupId>org.piwik.java.tracking</groupId>
    <artifactId>piwik-java-tracker</artifactId>
    <version>1.0.1</version>
</dependency>

Any advice?

mattab commented 8 years ago

Hi @jahudi

We are currently looking for a maintainer and developer to manage the Piwik JAVA Tracker SDK. In case you are interested to collaborate on this SDK please let us know! We would welcome your help and pull request to make the SDK better and high quality :+1:

mattab commented 8 years ago

@bcsorba Sorry I didn't meant to say we are looking for maintainer, since obviously you are maintaining the SDK already :-) I got confused with our iOS SDK where we would love to find new maintainers. Thank you for your work!

bcsorba commented 8 years ago

@mattab Haha not a problem at all and don't worry about it! Easy mistake to make :)

@jahudi Thanks for pointing this out - I was able to implement what I think is a workaround, but this highlights a problem that I created by making EcommerceItem a subclass of JsonValue. By allowing setters on EcommerceItem, this seems to go against the specs for JsonArray as found on https://jsonp.java.net/. A true fix to this may have to come through deprecating this class or its setters and potentially moving towards a different url construction pattern in 2.0

bcsorba commented 8 years ago

Released in v1.0.2

jahudi commented 8 years ago

@bcsorba Glad to help. Thanks for the quick fix!