Android:使用ArrayList将JSON数据读入类中

时间:2014-08-01 03:12:48

标签: java android json azure gson

我正在使用包含GSON的Azure SDK将Microsoft Azure的查询结果存储到类中。我一直在遵循此示例中的说明:http://blogs.msdn.com/b/carlosfigueira/archive/2013/06/19/custom-api-in-azure-mobile-services-client-sdks.aspx

所以我能够将一个简单的例子保存到所有字符串的类中。但是,当我尝试将数据存储到包含ArrayList的混合类型的类时,我遇到了障碍。这是我的班级:

class clsUser {
    public String UserID;
    public String UserName;
    public String UserStatus;
    public ArrayList<String> UserEmails;
    //Constructors, methods, etc
}

clsUser的一个实例可以有0个或多个UserEmails,所以我使用了一个ArrayList。我应该说我在我的应用程序中一直使用这个类 - 拉JSON数据是新的,但我想走这条路。

JsonObject看起来像这样:

{"UserID":85,"UserName":null,"UserEmails":"Call.Me@Maybe.org","UserStatus":"T"}

我也尝试过调整API,以便返回以下各项:

{"UserID":85,"UserName":null,"UserEmails":"[Call.Me@Maybe.org]","UserStatus":"T"}

{"UserID":85,"UserName":null,"UserEmails":"[{Call.Me@Maybe.org}]","UserStatus":"T"}

我收到错误“预期BEGIN_ARRAY但是STRING”,我知道这意味着它将“Call.Me@Maybe.org”视为字符串,但是如何才能进入ArrayList字段?:< / p>

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)

我认为错误是在数据或类中,但这是我为了完整性而使用的代码。在错误出现之前,没有任何日志有机会运行:

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());
        }
    }
});

任何人都有过这种混合课程的经历吗?

1 个答案:

答案 0 :(得分:0)

我没有针对这种情况对此进行测试(自8月份以来,我提出了一种我不想再访问的解决方法)。

但是我认为这里的关键是创建一个自定义反序列化器来告诉Gson如何解释数据。

请参阅:Custom JSON deserializer using Gson

然后使用mClient.registerSerializer方法