2014-09-20 2 views
0

나는 현재 Android 애플리케이션을 작성하는 중입니다. 여기 웹 서비스에서 다음 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" 
      } 
     ] 
    } 
} 

다음은 내가 시도한 것입니다. 중첩 루프의 두 번째, 세 번째 ... 루프에 대한 출력은 다시 0 인덱스에서 시작되었습니다. 어떤 생각이라도 도와주세요.

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

먼저

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

이 모두가 동일하다면, 추가,이 두 날짜를 비교, String.try이 코드를 반환 JSON에 createdDate와 메시지의 날짜를 비교 특정 날짜 채팅 내역의 내용,

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

코드가 잘 작성되지 않은 것처럼 보입니다. 나는 시도했다. 그러나 여기 Format.parse (a1)에서 "메서드 구문 (String)이 형식 형식에 대해 정의되지 않았다"라고 말했습니다. – Daroath

+0

코드 전에 formet을 추가하지 않았고, bcs 코드를 추가 할 공간을 남겨 둡니다. , 어쨌든 나는 위의 코드를 편집했다. – prakash

관련 문제