串联期间多次插入?

时间:2011-10-07 10:18:45

标签: java android string json

我有一个JSONObject,我已经转换为JSON数组,然后将其进一步解析为单独的字符串。我已将这些连接起来形成一个新字符串。这是我用过的代码。

            for(int n = 0; n<newtrs_array.length();n++){
                JSONObject object = newtrs_array.getJSONObject(n);
                String newcompanyid = object.getString("companyid");
                String newusername = object.getString("username");
                String newdate = object.getString("date");
                String newClientId = object.getString("clientid");
                String newprojectId = object.getString("projectid");
                String newniv1 = object.getString("niv1");
                String newniv2 = object.getString("niv2");
                String newworktypeid = object.getString("worktypeid");
                String newtimetypeid = object.getString("timetypeid");
                String newhours = object.getString("hours");
                String newcomment = object.getString("comment");
                String newprivatecomment = object.getString("privatecomment");
                String newopen = object.getString("open");
                String newreportid = object.getString("reportid");

                newtrsArray += newcompanyid+"#"+newusername+"#"+newdate+"#"
                               +newClientId+"#"+newprojectId
                               +"#"+newniv1+"#"+newniv2+"#"+newworktypeid
                               +"#"+newtimetypeid+"#"+newhours+"#"+newcomment
                               +"#"+newprivatecomment+"#"+newopen+"#"+newreportid;
                        }
            }

这是用于拆分newtrsarray的代码

        for(int n=0; n<split_frombufferedValue.length; n++){
            if(split_frombufferedValue[n] != null){
            try{
                //Log.i(TAG, "***********%%%%%%%%%"+split_frombufferedValue[1]);
                newtrsArray = split_frombufferedValue[0].toString();  
                newschemaArray = split_frombufferedValue[1].toString();
                Log.i(TAG, "************* "+newschemaArray);
                newdeletedtrsArray = split_frombufferedValue[2].toString();
                String newcreatedtrs_array = split_frombufferedValue[3].toString();
                String newsync_reponse = split_frombufferedValue[4].toString();
                String newmodtrs_array = split_frombufferedValue[5].toString();

            } catch(Exception e){
                e.printStackTrace();
            }
        }

        }
            split_newtrsArray = newtrsArray.split("#");
                    split_newdeletedtrsArray = newdeletedtrsArray.split("@@@@");
            split_newschemaArray = newschemaArray.split("%%%%");
            Log.i(TAG, "############# "+split_newtrsArray[0]+" -length "+split_newtrsArray.length);

    }

我的问题是: newtrsarray 的大小约为4000或更多。这导致“#”被插入两次甚至三次。如下图所示

&&--Second activty:HTTPClient--**(824): !!!!!------UTB17#DA#2011-10-10#1000#363636#10##105#30#5###no#30667UTB17#DA#2011-09-12#1000#363636#10##100##6##Not Available#yes#31659UTB17#DA#2011-09-13#1000#363636#10##100##2###yes#31665

如何解决此问题。必须将值连接到一个字符串,然后在另一个函数中拆分。然后必须将它们插入数据库中。希望我能够清楚地解释我的问题。

干杯!

1 个答案:

答案 0 :(得分:1)

我认为你的问题是在你再次插入时出现的,当数组有一个null元素时,检查if语句并检查它是否为null。这样做:

                if(newcompanyid.equals("") || newcompanyid == null || newusername.equals("") || newusername == null 
                        || newdate.equals("") || newdate == null || newClientId.equals("") || newClientId == null || 
                        newprojectId.equals("") || newprojectId == null || newniv1.equals("") || newniv1 == null || 
                        newniv2.equals("") || newworktypeid.equals("") || newworktypeid == null ||newtimetypeid.equals("") 
                        || newtimetypeid == null || newhours.equals("") ||newhours == null ||newcomment.equals("") 
                        ||newcomment == null ||newprivatecomment.equals("") ||newprivatecomment == null||newopen.equals("")
                        ||newopen == null||newreportid.equals("") || newreportid == null)