I'm using the Azure SDK which includes GSON to store query results from Microsoft Azure into a class. I've been following the instructions from this example: http://blogs.msdn.com/b/carlosfigueira/archive/2013/06/19/custom-api-in-azure-mobile-services-client-sdks.aspx
So I was able to save a simple example into a class of all Strings. However I hit a roadblock when trying to store data into a class with mixed types including an ArrayList. Here's my class:
class clsUser {
public String UserID;
public String UserName;
public String UserStatus;
public ArrayList<String> UserEmails;
//Constructors, methods, etc
}
An instance of clsUser can have 0 or many UserEmails, so I use an ArrayList. I should say that I've been using this class in my app - pulling JSON data is new, but I'd like to go this route.
The JsonObject looks like this:
{"UserID":85,"UserName":null,"UserEmails":"Call.Me@Maybe.org","UserStatus":"T"}
I've also tried adjusting the API so it returns each of these:
{"UserID":85,"UserName":null,"UserEmails":"[Call.Me@Maybe.org]","UserStatus":"T"}
{"UserID":85,"UserName":null,"UserEmails":"[{Call.Me@Maybe.org}]","UserStatus":"T"}
And I'm getting an error "Expected BEGIN_ARRAY but was STRING" which I know means it sees "Call.Me@Maybe.org" as a String, but how can I make it go into the ArrayList field?:
07-31 22:59:18.776 27842-27842/com.myapp.app.debug E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.myapp.app.debug, PID: 27842
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
at com.google.gson.Gson.fromJson(Gson.java:795)
at com.google.gson.Gson.fromJson(Gson.java:859)
at com.google.gson.Gson.fromJson(Gson.java:832)
at com.microsoft.windowsazure.mobileservices.JsonEntityParser.parseResults(JsonEntityParser.java:62)
at com.microsoft.windowsazure.mobileservices.MobileServiceClient$4.onCompleted(MobileServiceClient.java:615)
at com.microsoft.windowsazure.mobileservices.MobileServiceClient$5.onResponse(MobileServiceClient.java:710)
at com.microsoft.windowsazure.mobileservices.MobileServiceClient$6.onPostExecute(MobileServiceClient.java:825)
at com.microsoft.windowsazure.mobileservices.MobileServiceClient$6.onPostExecute(MobileServiceClient.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5487)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING
at com.google.gson.internal.bind.JsonTreeReader.expect(JsonTreeReader.java:139)
at com.google.gson.internal.bind.JsonTreeReader.beginArray(JsonTreeReader.java:58)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:79)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
at com.google.gson.Gson.fromJson(Gson.java:795)
at com.google.gson.Gson.fromJson(Gson.java:859)
at com.google.gson.Gson.fromJson(Gson.java:832)
at com.microsoft.windowsazure.mobileservices.JsonEntityParser.parseResults(JsonEntityParser.java:62)
at com.microsoft.windowsazure.mobileservices.MobileServiceClient$4.onCompleted(MobileServiceClient.java:615)
at com.microsoft.windowsazure.mobileservices.MobileServiceClient$5.onResponse(MobileServiceClient.java:710)
at com.microsoft.windowsazure.mobileservices.MobileServiceClient$6.onPostExecute(MobileServiceClient.java:825)
at com.microsoft.windowsazure.mobileservices.MobileServiceClient$6.onPostExecute(MobileServiceClient.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5487)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
I think the error is either in the data or the class, but here's the code I'm using for the sake of completeness. None of the logs get a chance to run before the error comes up:
List<Pair<String, String>> params = new ArrayList<Pair<String, String>>();
params.add(new Pair<String, String>("email", emailtext));
params.add(new Pair<String, String>("token", Token));
Azure.invokeApi("user/login", "GET", params, clsUser.class, new ApiOperationCallback<clsUser>() {
@Override
public void onCompleted(clsUser result, Exception e, ServiceFilterResponse response) {
if (e == null) {
Log.d("Login GetToken","Azure success: ID=" + result.getUserID());
Log.d("Login GetToken","Azure success: " + result.getAll().toString());
currentUser = result;
} else {
Log.d("Login GetToken","Azure Error: " + e.toString());
}
}
});
Anyone have experience with this kind of mixed class?