Using the Http.ExecuteRequestAsJson method results in a ClassCastException since JSONArray cannot be cast to JSONObject.
The thing that also bothers me is the fact that the execution of the request itself is sync, while the conversion to JSON or String etc. is async.
This bothers me since im willing to do this all async and therefore in Android i’m using an AsyncTask, where in iOS I will use something like dispatch_async. But going async while already executing in the background is kinda weird.
Hope I informed you well enough about the issue i’m facing.
I used jsonLint to check if it was valid json and it is. It’s just a generated REST API in Java with JAX-RS returning that response.
Im using a shared silver project that has the request code and i use the code in Android on Android 6.0 emulator. Or did you mean my development IDE, since that’s Visual studio 2015.
Hmm, ok. We’ll need to expand/rework the Sugar API to allow non-Object items as root element, then. This will be a breaking change :(. O’ll see what i can do, and when. In the mean time, isn you can change your API to return an object, that would be the bets workaround.
i meant platform — i.e. Android (opposed to .NET or Cocoa), yes. That was before i realized what the issue was; this problem will actually affect all 3 platforms right now, and its a fundamental design issue wit the JsonDocument class, not a small “bug”.
@Koen_Kraak
I didn’t know what file had the tantative fix or how to use the Sugar project from http://github.com/remobjects/Sugar since it has no .jar file to use as reference. (Sorry im still new)
What I did and what worked for me was using the Http.ExecuteRequestAsString method and with the following utility class to wrap JsonArray’s in a JsonObject with a prefix for example instead of the json supplied in my first post it will look like:
{
“data”:[
{
“description”: “Population on 1 January”,
“id”: 1,
“maxValue”: 520000000,
“minValue”: 0,
“unit”: “people”
}
]
}
// Utility class I wrote for wrapping:
public class JsonUtils{
// Method to convert a string that is in json format to a JsonDoc
// without the ClassCastException
// @NOTE: no check performed to identify if valid JSON
// @param jsonArrayKey, The key to add if root is a JsonArray
public static func stringToJsonDoc(jsonString: sugar.String, jsonArrayKey: sugar.String) -> sugar.json.JsonDocument{
var jsonFormattedString = jsonString
// Make sure it can be converted to a JsonDocument by making it an JsonObject if it is a JsonArray
if(isJsonArray(jsonString)){
jsonFormattedString = "{\"" + jsonArrayKey + "\"" + ":" + jsonFormattedString + "}"
}
return sugar.json.JsonDocument.FromString(jsonFormattedString)
}
// Method to check if a json formatted string is a JsonArray
private static func isJsonArray(jsonString: sugar.String) -> Bool {
return jsonString.getChars(0) == "["
}
}
So the usage of it will be for example:
// response is sugar.HttpResponseContent<sugar.String!>! which is returned by the method Http.ExecuteRequestAsString
var jsonDoc: sugar.json.JsonDocument = JsonUtils.stringToJsonDoc(response.Content, jsonArrayKey:“data”)
Hope this helps someone who is facing the same issue
the github repo has the source; you’ll want the develop branch, and rebuild the Sugar.jar project manually. That, or wait for the next beta drop, Friday.
Ah thanks, I signed up but the download section is a bit confusing. It says Silver for Android, Silver for .NET and SIlver for Cocao with Visual studio.
Which one should I grab, since I thought Silver was cross platform (Once I create a shared project) and I need both IOS and Android projects that use the same shared project.
Besides that, does it includes the Sugar.Data to make use of SQLite?
You can grab either, it’s all there same download. It’s just that the beta licenses is granted a separate separate licenses for each platform, thats why it shows three time.s Sugar.Data is included, yes.
Ah thanks, I can’t seem to use Sugar.Data for SQLite, since it just doesn’t recognizes it. Do I have to add the reference or something? If so where can I find the jar for Android?
Yah well im using visual studio and auto completion is not there for SQLite or sugar.data.SQLite so I cannot create an instance. When I do it results in ‘unknown type’
The way you would add any other Reference: right click References, choose Add References, and add Sugar.Data.jar (Android) orc libSugarData.fx (iOS). See https://docs.elementscompiler.com/Projects/References/ for details.
Yah i’m aware of how to add it. But I don’t have the Sugar.Data.jar or libSugarData.fx. I don’t know how to get them since they aren’t included in the installation.
C:\Program Files (x86)\RemObjects Software\Elements\Sugar\Cooper\Android
only contains sugar.jar and not sugar.data.jar
I hope this file can be uploaded somewhere so I can download it, since I can’t build the Sugar solution from the GitHub repo either since it gives me an error about something like ‘obsolete not allowed’.