在使用Android API 18进行循环后,某些数组值会丢失,但在API 22

时间:2016-02-29 15:12:43

标签: android arrays json sqlite api

我试图在登录期间检索用户的云联系人并将其保存在本地SQLite中,一切都很好,直到我必须将信息写入数据库,但使用带有API的电话18 (4.3)似乎专用阵列的某些值丢失了,而另一款带有API 22(5.1.1)的手机也不会发生这种情况。我试图弄清楚这种行为的原因,但经过一些测试我不知道还有什么做。但是,我注意到一个奇怪的事情,当我使用API​​ 18启动应用程序时,我在Android Studio日志中收到了一些奇怪的警告,我做了一些研究,但我不知道它是否相关

活动中的代码:

            DatabaseNumbersHandler dbn = new DatabaseNumbersHandler(getApplicationContext());

            Integer jsonLen = json.length();

            String[][] id = new String[jsonLen][4];
            String[][] number = new String[jsonLen][4];
            String[][] title = new String[jsonLen][4];
            String[][] description = new String[jsonLen][4];
            String[][] time_creation = new String[jsonLen][4];

            Integer stop = 0;

            Integer iter_position = 0;

            for (int i = 0; i < jsonLen; i++) {
                Log.v("ITER POS", String.valueOf(iter_position));
                JSONObject object = json.optJSONObject(i);
                Iterator<String> iterator = object.keys();

                while (iterator.hasNext()) {
                    try {
                        String currentKey = iterator.next();
                        String currentValue = object.getString(currentKey);

                        // If the key name is not numerical go on
                        if (!currentKey.matches("[-+]?\\d*\\.?\\d+")) {
                            Log.v("KEY_CLEAN", currentKey);

                            // If an "error" key exists show the alert and stop the db writing
                            if (currentKey.matches(KEY_ERROR)) {
                                if (Integer.parseInt(currentValue) == 1){
                                    // If no number exists
                                    final String no_contacts = getResources().getString(R.string.no_contacts_imported);

                                    Toast.makeText(getApplicationContext(),
                                            no_contacts, Toast.LENGTH_LONG).show();
                                    stop = 1;

                                    break;
                                } else if (Integer.parseInt(currentValue) == 2) {
                                    // If the numbers are not imported due to an error
                                    final String contacts_error = getResources().getString(R.string.error_contacts_not_imported);

                                    Toast.makeText(getApplicationContext(),
                                            contacts_error, Toast.LENGTH_LONG).show();
                                    stop = 1;

                                    break;
                                } else {
                                    // Undefined error
                                    final String undefined_error = getResources().getString(R.string.undefined_error);

                                    Toast.makeText(getApplicationContext(),
                                            undefined_error, Toast.LENGTH_LONG).show();
                                    stop = 1;

                                    break;
                                }
                            } else {
                                switch(currentKey){
                                    case "id":
                                        id[i][iter_position] = currentValue;
                                        Log.v("ID", id[i][iter_position]);
                                        break;
                                    case "number":
                                        number[i][iter_position] = currentValue;
                                        Log.v("NUMBER", number[i][iter_position]);
                                        break;
                                    case "title":
                                        title[i][iter_position] = currentValue;
                                        Log.v("TITLE", title[i][iter_position]);
                                        break;
                                    case "description":
                                        description[i][iter_position] = currentValue;
                                        Log.v("DESC", description[i][iter_position]);
                                        break;
                                    case "time_creation":
                                        time_creation[i][iter_position] = currentValue;
                                        Log.v("TIME", time_creation[i][iter_position]);
                                        iter_position++;
                                        break;
                                }
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            if(stop.equals(0)) {
                for (int i = 0; i < jsonLen; i++) {
                    Log.v("WRITE ID", String.valueOf(id[i][i]));
                    Log.v("WRITE NUMBER", String.valueOf(number[i][i]));
                    Log.v("WRITE TITLE", String.valueOf(title[i][i]));
                    Log.v("WRITE DESC", String.valueOf(description[i][i]));
                    Log.v("WRITE TIME", String.valueOf(time_creation[i][i]));
                    dbn.addNumber(id[i][i], user_id_get, number[i][i], title[i][i], description[i][i], time_creation[i][i]);
                }
            }

使用API​​ 18生成的日志(Android 4.3)

注意:第一行是从云返回的json数组

02-29 14:55:57.049 2847-7742/com.myapp E/JSON: [{"id":2,"0":2,"number":3384729034,"1":3384729034,"title":"Mark","2":"Mark","description":"friend","3":"friend","time_creation":1449830672,"4":1449830672},{"id":3,"0":3,"number":3497812429,"1":3497812429,"title":"Erik","2":"Erik","description":"brother","3":"brother","time_creation":1455030339,"4":1455030339}]
02-29 14:55:57.064 2847-2847/com.myapp V/ITER POS: 0
02-29 14:55:57.069 2847-2847/com.myapp V/KEY_CLEAN: id
02-29 14:55:57.069 2847-2847/com.myapp V/ID: 2
02-29 14:55:57.074 2847-2847/com.myapp V/KEY_CLEAN: title
02-29 14:55:57.074 2847-2847/com.myapp V/TITLE: Mark
02-29 14:55:57.074 2847-2847/com.myapp V/KEY_CLEAN: time_creation
02-29 14:55:57.074 2847-2847/com.myapp V/TIME: 1449830672
02-29 14:55:57.079 2847-2847/com.myapp V/KEY_CLEAN: number
02-29 14:55:57.079 2847-2847/com.myapp V/NUMBER: 3384729034
02-29 14:55:57.079 2847-2847/com.myapp V/KEY_CLEAN: description
02-29 14:55:57.079 2847-2847/com.myapp V/DESC: friend
02-29 14:55:57.079 2847-2847/com.myapp V/ITER POS: 1
02-29 14:55:57.084 2847-2847/com.myapp V/KEY_CLEAN: id
02-29 14:55:57.084 2847-2847/com.myapp V/ID: 3
02-29 14:55:57.084 2847-2847/com.myapp V/KEY_CLEAN: title
02-29 14:55:57.084 2847-2847/com.myapp V/TITLE: Erik
02-29 14:55:57.089 2847-2847/com.myapp V/KEY_CLEAN: time_creation
02-29 14:55:57.089 2847-2847/com.myapp V/TIME: 1455030339
02-29 14:55:57.089 2847-2847/com.myapp V/KEY_CLEAN: number
02-29 14:55:57.089 2847-2847/com.myapp V/NUMBER: 3497812429
02-29 14:55:57.089 2847-2847/com.myapp V/KEY_CLEAN: description
02-29 14:55:57.094 2847-2847/com.myapp V/DESC: brother
02-29 14:55:57.094 2847-2847/com.myapp V/WRITE ID: 2
02-29 14:55:57.094 2847-2847/com.myapp V/WRITE NUMBER: null
02-29 14:55:57.094 2847-2847/com.myapp V/WRITE TITLE: Mark
02-29 14:55:57.094 2847-2847/com.myapp V/WRITE DESC: null
02-29 14:55:57.094 2847-2847/com.myapp V/WRITE TIME: 1449830672
02-29 14:55:57.104 2847-2847/com.myapp V/WRITE ID: 3
02-29 14:55:57.104 2847-2847/com.myapp V/WRITE NUMBER: null
02-29 14:55:57.104 2847-2847/com.myapp V/WRITE TITLE: Erik
02-29 14:55:57.104 2847-2847/com.myapp V/WRITE DESC: null
02-29 14:55:57.104 2847-2847/com.myapp V/WRITE TIME: 1455030339

使用API​​ 22生成的日志(Android 5.1)

注意:第一行是从云返回的json数组

02-29 15:05:10.081 23832-24626/com.myapp E/JSON: [{"id":2,"0":2,"number":3384729034,"1":3384729034,"title":"Mark","2":"Mark","description":"friend","3":"friend","time_creation":1449830672,"4":1449830672},{"id":3,"0":3,"number":3497812429,"1":3497812429,"title":"Erik","2":"Erik","description":"brother","3":"brother","time_creation":1455030339,"4":1455030339}]
02-29 15:05:10.081 23832-23832/com.myapp V/ITER POS: 0
02-29 15:05:10.081 23832-23832/com.myapp V/KEY_CLEAN: id
02-29 15:05:10.081 23832-23832/com.myapp V/ID: 2
02-29 15:05:10.081 23832-23832/com.myapp V/KEY_CLEAN: number
02-29 15:05:10.081 23832-23832/com.myapp V/NUMBER: 3384729034
02-29 15:05:10.081 23832-23832/com.myapp V/KEY_CLEAN: title
02-29 15:05:10.081 23832-23832/com.myapp V/TITLE: Mark
02-29 15:05:10.091 23832-23832/com.myapp V/KEY_CLEAN: description
02-29 15:05:10.091 23832-23832/com.myapp V/DESC: friend
02-29 15:05:10.091 23832-23832/com.myapp V/KEY_CLEAN: time_creation
02-29 15:05:10.091 23832-23832/com.myapp V/TIME: 1449830672
02-29 15:05:10.091 23832-23832/com.myapp V/ITER POS: 1
02-29 15:05:10.091 23832-23832/com.myapp V/KEY_CLEAN: id
02-29 15:05:10.091 23832-23832/com.myapp V/ID: 3
02-29 15:05:10.091 23832-23832/com.myapp V/KEY_CLEAN: number
02-29 15:05:10.091 23832-23832/com.myapp V/NUMBER: 3497812429
02-29 15:05:10.091 23832-23832/com.myapp V/KEY_CLEAN: title
02-29 15:05:10.091 23832-23832/com.myapp V/TITLE: Erik
02-29 15:05:10.101 23832-23832/com.myapp V/KEY_CLEAN: description
02-29 15:05:10.101 23832-23832/com.myapp V/DESC: brother
02-29 15:05:10.101 23832-23832/com.myapp V/KEY_CLEAN: time_creation
02-29 15:05:10.101 23832-23832/com.myapp V/TIME: 1455030339
02-29 15:05:10.101 23832-23832/com.myapp V/WRITE ID: 2
02-29 15:05:10.101 23832-23832/com.myapp V/WRITE NUMBER: 3384729034
02-29 15:05:10.101 23832-23832/com.myapp V/WRITE TITLE: Mark
02-29 15:05:10.101 23832-23832/com.myapp V/WRITE DESC: friend
02-29 15:05:10.101 23832-23832/com.myapp V/WRITE TIME: 1449830672
02-29 15:05:10.111 23832-23832/com.myapp V/WRITE ID: 3
02-29 15:05:10.111 23832-23832/com.myapp V/WRITE NUMBER: 3497812429
02-29 15:05:10.111 23832-23832/com.myapp V/WRITE TITLE: Erik
02-29 15:05:10.111 23832-23832/com.myapp V/WRITE DESC: brother
02-29 15:05:10.111 23832-23832/com.myapp V/WRITE TIME: 1455030339

正如你可以看到&#34;数字&#34;的数组值和&#34;描述&#34;就在使用API​​ 18进行db编写之前,使用API​​ 22

是正确的

当我使用API​​ 18启动应用程序时,我注意到的奇怪警告如下:

02-29 14:50:57.264 2847-2847/? D/dalvikvm: Late-enabling CheckJNI
02-29 14:50:57.469 2847-2847/com.myapp W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
02-29 14:50:57.469 2847-2847/com.myapp I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onSearchRequested
02-29 14:50:57.469 2847-2847/com.myapp W/dalvikvm: VFY: unable to resolve interface method 18175: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
02-29 14:50:57.469 2847-2847/com.myapp D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
02-29 14:50:57.469 2847-2847/com.myapp I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onWindowStartingActionMode
02-29 14:50:57.474 2847-2847/com.myapp W/dalvikvm: VFY: unable to resolve interface method 18179: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
02-29 14:50:57.474 2847-2847/com.myapp D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
02-29 14:50:57.519 2847-2847/com.myapp I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
02-29 14:50:57.519 2847-2847/com.myapp W/dalvikvm: VFY: unable to resolve virtual method 490: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
02-29 14:50:57.519 2847-2847/com.myapp D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
02-29 14:50:57.524 2847-2847/com.myapp I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
02-29 14:50:57.524 2847-2847/com.myapp W/dalvikvm: VFY: unable to resolve virtual method 512: Landroid/content/res/TypedArray;.getType (I)I
02-29 14:50:57.524 2847-2847/com.myapp D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
02-29 14:50:57.564 2847-2847/com.myapp I/dalvikvm: Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
02-29 14:50:57.564 2847-2847/com.myapp W/dalvikvm: VFY: unable to resolve virtual method 453: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
02-29 14:50:57.564 2847-2847/com.myapp D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
02-29 14:50:57.564 2847-2847/com.myapp I/dalvikvm: Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
02-29 14:50:57.564 2847-2847/com.myapp W/dalvikvm: VFY: unable to resolve virtual method 455: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
02-29 14:50:57.564 2847-2847/com.myapp D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
02-29 14:50:57.659 2847-2847/com.myapp D/libEGL: loaded /system/lib/egl/libEGL_mali.so
02-29 14:50:57.664 2847-2847/com.myapp D/libEGL: loaded /system/lib/egl/libGLESv1_CM_mali.so
02-29 14:50:57.669 2847-2847/com.myapp D/libEGL: loaded /system/lib/egl/libGLESv2_mali.so
02-29 14:50:57.729 2847-2847/com.myapp D/OpenGLRenderer: Enabling debug mode 0

1 个答案:

答案 0 :(得分:0)

使用另一种迭代方法解决:

    for(int i = 0; i < json.length(); i++){
                String error = null;
                try {
                    error = json.getJSONObject(i).getString("error");
                } catch (JSONException e) {
                    //e.printStackTrace();
                }

                // If there are no errors go on
                if(error == null) {
                    try {
                       dbn.addExtNumber(json.getJSONObject(i).getString("id"), user_id_get, json.getJSONObject(i).getString("number"), json.getJSONObject(i).getString("title"), json.getJSONObject(i).getString("description"), json.getJSONObject(i).getString("time_creation"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else if (Integer.parseInt(error) == 1){
                        final String no_contacts = getResources().getString(R.string.no_contacts_imported);

                        Toast.makeText(getApplicationContext(),
                                no_contacts, Toast.LENGTH_LONG).show();
                        break;
                } else if (Integer.parseInt(error) == 2) {
                        final String contacts_error = getResources().getString(R.string.error_contacts_not_imported);

                        Toast.makeText(getApplicationContext(),
                                contacts_error, Toast.LENGTH_LONG).show();
                        break;
                } else {
                        final String undefined_error = getResources().getString(R.string.undefined_error);

                        Toast.makeText(getApplicationContext(),
                                undefined_error, Toast.LENGTH_LONG).show();
                        break;
                }
    }