从json android向db添加数据的更快方法

问题描述

我的字典应用程序有大约70,000个单词,字典,同义词和反义词列表。 这些都存储在我的JSON文件中。 安装应用程序后,必须将这些数据从JSON文件传输到我的本地数据库中。

try {
                    Log.d(LOG_TAG,"parsing json");
                    InputStream inputStream = mContext.getAssets().open("worddictionary.json");
                    int size = inputStream.available();
                    Log.d(LOG_TAG,"parsing json size: "+size);
                    byte[] buffer = new byte[size];
                    inputStream.read(buffer);
                    inputStream.close();
                    json = new String(buffer,"UTF-8");

                    JSONArray jsonArray = new JSONArray(json);

                    for (int i=0;i<jsonArray.length();i++){

                        notification.setProgress(jsonArray.length(),i,false);
                        notificationmanager.notify(1,notification.build());

                        JSONObject obj = jsonArray.getJSONObject(i);
                        word = obj.getString("word");
                        deFinition = obj.getString("deFinition");
                        audiourl = obj.getString("audiourl");
                        synonym = obj.getString("synonym");
                        antonym = obj.getString("antonym");

                        ContentValues values = new ContentValues();
                        values.put("word",word);
                        values.put("deFinition",deFinition);
                        values.put("audiourl",audiourl);
                        values.put("synonyms",synonym);
                        values.put("antonyms",antonym);

                        long id = mDatabase.insert(WordDictionaryContract.WordDictionaryEntry.TABLE_NAME,null,values);

                        if (id==-1){
                            Log.d(LOG_TAG,"insert Failed: "+word);
                        } else{
                            Log.d(LOG_TAG,"insert success id: "+id);
                        }

                    }



                } catch (IOException e) {
                    e.printstacktrace();
                    Log.d(LOG_TAG,"Read json error: "+e);
                } catch (JSONException e) {
                    Log.d(LOG_TAG,"Parse json error: "+e.getStackTrace());
                    e.printstacktrace();
                }

上面的代码从JSON文件获取数据并插入数据库。 这在我的前台服务中发生,并带有指示加载进度的通知

此过程大约需要15-20分钟才能将数据插入数据库

还有其他方法可以更快地将这些数据插入数据库吗?

我正在使该应用脱机,因此不使用在线服务器。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)