2014-09-20 27 views
0

我目前正在创建Android聊天应用程序。在这里,我从Web服务中获得了以下json。我想要的是创建聊天的部分日期,以便聊天是在特定日期下的组。如何按日期将聊天记录分组

{ 
    "messages":{ 
     "rows":[ 
      { 
       "createdDate":"2014-09-13 13:14:07", 
       "cID":"36", 
       "message":"again with me this is not yet the true chat on android.", 
       "mDate":"2014-09-13 11:14:07", 
       "senderID":"100000876519562", 
       "readStatus":"2", 
       "ID":"156", 
       "recieverID":"100001031525354" 
      }, 
      { 
       "createdDate":"2014-09-15 12:57:44", 
       "cID":"36", 
       "message":"this is so long long text on the chat application for me", 
       "mDate":"2014-09-15 10:57:44", 
       "senderID":"100000876519562", 
       "readStatus":"2", 
       "ID":"158", 
       "recieverID":"100001031525354" 
      }, 
      { 
       "createdDate":"2014-09-15 13:32:42", 
       "cID":"36", 
       "message":"is it OK, if I try more and more.", 
       "mDate":"2014-09-15 11:32:42", 
       "senderID":"100001031525354", 
       "readStatus":"2", 
       "ID":"159", 
       "recieverID":"100000876519562" 
      }, 
      { 
       "createdDate":"2014-09-15 18:38:57", 
       "cID":"36", 
       "message":"what if I typed so long long text over here again and again.", 
       "mDate":"2014-09-15 16:38:57", 
       "senderID":"100000876519562", 
       "readStatus":"2", 
       "ID":"166", 
       "recieverID":"100001031525354" 
      }, 
      { 
       "createdDate":"2014-09-16 11:22:13", 
       "cID":"36", 
       "message":"hi this is something uncommon", 
       "mDate":"2014-09-16 09:22:13", 
       "senderID":"100000876519562", 
       "readStatus":"2", 
       "ID":"167", 
       "recieverID":"100001031525354" 
      }, 
      { 
       "createdDate":"2014-09-16 13:21:55", 
       "cID":"36", 
       "message":"it's really", 
       "mDate":"2014-09-16 11:21:55", 
       "senderID":"100000876519562", 
       "readStatus":"2", 
       "ID":"170", 
       "recieverID":"100001031525354" 
      }, 
      { 
       "createdDate":"2014-09-16 17:25:10", 
       "cID":"36", 
       "message":"yes sure", 
       "mDate":"2014-09-16 15:25:10", 
       "senderID":"100000876519562", 
       "readStatus":"2", 
       "ID":"183", 
       "recieverID":"100001031525354" 
      } 
     ] 
    }, 
    "section":{ 
     "rows":[ 
      { 
       "MessageDate":"2014-09-13", 
       "Num":"1" 
      }, 
      { 
       "MessageDate":"2014-09-15", 
       "Num":"3" 
      }, 
      { 
       "MessageDate":"2014-09-16", 
       "Num":"3" 
      } 
     ] 
    } 
} 

这是我试过的。输出为嵌套循环的第二,第三...循环再次从零索引开始。任何想法请帮助。

try { 
      JSONObject jObj = new JSONObject(content); 
      JSONObject obj = jObj.getJSONObject("messages"); 
      JSONArray arr = obj.getJSONArray("rows"); 

      JSONObject objSec = jObj.getJSONObject("section"); 
      JSONArray arrSec = objSec.getJSONArray("rows"); 

      List<MsgChat> msgList = new ArrayList<MsgChat>(); 
      MsgChat ch = new MsgChat(); 
      for (int i = 0; i < arrSec.length(); i++) { 
       JSONObject objHis = arrSec.getJSONObject(i); 
       ch.setChatSection(objHis.getString("MessageDate")); 
       Log.d("Number of Message", objHis.getString("Num")); 

       int chatNum = arrSec.getJSONObject(i).getInt("Num"); 

       for(int j = 0; j < chatNum; j++) { 
        JSONObject messages = arr.getJSONObject(j); 
        ch.setMsgId(messages.getInt("ID")); 
        ch.setChatMsg(messages.getString("message")); 
        Log.d("Message", messages.getString("message")); 
        ch.setChatDate(messages 
          .getString("createdDate")); 
        ch.setSenderId(Long.parseLong(messages 
          .getString("senderID"))); 
        msgList.add(ch); 
       } 
       msgList.add(ch); 
      } 

      return msgList; 
     } catch (JSONException e) { 
      e.printStackTrace(); 
      return null; 
     } 

回答

1

先比较留言日期createdDate在JSON返回String.try这个代码,

String a1 = objHis.getString("MessageDate"); 
String[] a21 = messages.getString("createdDate").split(" "); 
String a2 = a21[0].tostring; 

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
Date date1 = format.parse(a1); 
Date date2 = format.parse(a2); 

比较这两个日期,如果是双方平等相待,补充说,在特定日期的聊天记录内容,

if(date1.compareTo(date2)==0) 
{ 
    //add your chat history message for particular date 
} 
+0

你的代码似乎没有很好的准备。我尝试过,但在这里Format.parse(a1)说错误“方法解析(字符串)是未定义的类型格式” – Daroath 2014-09-25 01:39:12

+0

我没有添加代码之前formet,bcs你我留下空间自己添加代码,无论如何,我已经编辑了上面的代码。 – prakash 2014-09-25 04:16:19