×

Discussion Board

Results 1 to 11 of 11
  1. #1
    Regular Contributor
    Join Date
    Oct 2005
    Posts
    54

    PIM Api and fromSerialFormat()

    Hi,

    I am making an application which uses PIM API.I wanted to know that can i retrieve multiple contacts information from the server on to the handset at one go or we can only retrieve single contact information in one time.
    I saw in the PIM API documentation that the function fromSerialFormat() is used to only get one contact information at a time.Please tell me if iam right in understanding it or not .
    Is there any way we can get multiple Contacts info under the same contact list from the server and insert them into the handset.

    Regards,
    Divya

  2. #2
    Super Contributor
    Join Date
    Dec 2005
    Location
    Europe/Poland/Warsaw
    Posts
    1,697

    Re: PIM Api and fromSerialFormat()

    Hi Divya,

    taken from jsr documentation:
    This method reads only one complete data entry from the InputStream, leaving the InputStream object open and its character position on the character after the end of the entry, or at a position in the stream where an error is detected if the data stream is in an invalid or incomplete format. A single valid data entry in an InputStream results in the creation of one or more PIMItems (in some data formats more than one PIMItem may be in a single entry).
    for me it sounds like that:
    - one can provide multile vCards in input stream
    - before first call of that method input stream is positioned at "begin" tag start
    - calling that method reads bytes from "begin" up to first "end" tag and leaves input stream open
    - method returns one or more PIM items (contact(s), event(s), todo(s)
    - we can call that method again on still opened input stream until end of input stream is achieved or exception occurs,
    hope that someone will clarify it with more details,
    hth,
    regards,
    peter

  3. #3
    Regular Contributor
    Join Date
    Jan 2006
    Location
    India
    Posts
    87

    Re: PIM Api and fromSerialFormat()

    Hi divyas,
    Can u explain your problem in detail........What is the format of your contact in server..it is in database right.......try to make a format like xml string,download the xml usinng inputstream, parse the xml and save it to the handset..........................
    Whether i have understood the problem right or something else problem u r facing.
    Thanks & Regards
    kannan
    [kamalinfo2k2@yahoo.com]

  4. #4
    Super Contributor
    Join Date
    Dec 2005
    Location
    Europe/Poland/Warsaw
    Posts
    1,697

    Re: PIM Api and fromSerialFormat()

    Quote Originally Posted by divyas
    I wanted to know that can i retrieve multiple contacts information from the server on to the handset at one go or we can only retrieve single contact information in one time.
    I saw in the PIM API documentation that the function fromSerialFormat() is used to only get one contact information at a time.Please tell me if iam right in understanding it or not .
    Is there any way we can get multiple Contacts info under the same contact list from the server and insert them into the handset.
    Hello Divya,
    I was learnign today about various things related to PIM api and wondering about what is stated in pim docs I simply created test-case for importing vCards created from OutlookExpress address book into S60/S40 SDK emulators Contacts book,

    that is stated in vCard 2.1 specs:
    A vCard is a data stream consisting of one or more vCard objects. The individual vCard definitions can be identified and parsed within the datastream. The vCard data stream may exist as a persistent form in a file system, document management system, network connection between two network endpoints, or in any other digital transport that has an abstraction of a stream of bytes.
    http://www.imc.org/pdi/vcard-21.txt
    for me that match exactly what is described in JSR-75 PIM api docs,

    so following description from docs I've done following,
    I've exported 3 vCards from OutlookExpress adding them into resources folder in application .jar
    in test midlet I simply read all three vCards from .jar and merged them into single input stream to simulate a stream which does contain multiple vCards - that is somethin you asked about and something which is outlined in vCard specs,

    sample: "Adding FN Champtions into contact list":

    Code:
    Vector items = null;
            InputStream is = null;
            ByteArrayInputStream bis = null;
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            String[] vCardsNames = {"/resources/vcards/Peter Blazejewicz.vcf",
            "/resources/vcards/Juarez Junior.vcf",
            "/resources/vcards/Balagopal NKnair.vcf"};
            for(int i = 0;i<vCardsNames.length; i++){
                is = getClass().getResourceAsStream(vCardsNames[i]);
                if(is == null){
                    myForm.append("cannot read vCard from .jar\n");
                    myForm.append(vCardsNames[i]+"\n");
                }else{
                    writeCardToStream(is, out);
                }
                if(is != null){
                    try {
                        is.close();
                    } catch (IOException ex) {}
                }
                is = null;
            }
    Code:
    private void writeCardToStream(InputStream in, ByteArrayOutputStream out){
            int b;
            try{
                while((b = in.read())>-1){
                    out.write(b);
                }
            }catch(IOException ex){}
        }
    after merging vCards into single bytes stream I've used fromSerialFormat(..., ...) method as follows:
    Code:
    PIMItem[] newItems;
            bis = new ByteArrayInputStream(out.toByteArray());
            while(bis.available()> 0){
                newItems = convertBytesToItems(bis);
                if(newItems != null){
                    if(items == null){
                        items = new Vector(newItems.length);
                    }else{
                        items.ensureCapacity(items.capacity()+newItems.length);
                    }
                    for(int i = 0; i<newItems.length; i++){
                        items.addElement(newItems[i]);
                    }
                }
                // vCard from OutlookExpress ends with \u0013\u0010
                // and they must to be skipped
                if(bis.available()>=2){
                    bis.skip(2);
                }
            }
            if(bis != null){
                try {
                    bis.close();
                } catch (IOException ex) {}
            }
            PIMList pimList = null;
            String label = null;
            String value = null;
            Contact newContact = null;
            // are there new items?
            if(items.isEmpty() == false){
                Enumeration en = items.elements();
                while(en.hasMoreElements()){
                    PIMItem item = (PIMItem) en.nextElement();
                    if(item instanceof Contact){
                        if(pimList == null){
                            try {
                                pimList = PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE);
                            } catch (PIMException ex) {
                                ex.printStackTrace();
                                break;
                            }
                        }
                        if(pimList == null){
                            break;
                        }
                        newContact  = ((ContactList)pimList).importContact((Contact) item);
                        try {
                            //
                            // should commit be called?
                            //
                            newContact.commit();
                            myForm.append("New contact added:\n");
                        } catch (PIMException ex) {
                            ex.printStackTrace();
                            System.out.println("cannot save contact");
                            continue;
                        } catch(SecurityException ex){
                            System.out.println("PIM opened in read-olny mode");
                            break;
                        }
                        int[] fields = newContact.getFields();
                        int field;
                        for(int i = 0; i<fields.length; i++){
                            field = fields[i];
                            if(pimList.isSupportedField(field) == true){
                                if(pimList.getFieldDataType(field) == PIMItem.STRING){
                                    label = pimList.getFieldLabel(field);
                                    value = newContact.getString(field, 0);
                                    if(value != null){
                                        System.out.println(label+"\n"+value);
                                        myForm.append(label+"\n"+value+"\n");
                                    }
                                }
                            }
                        }
                        
                    }
                }
            }
            if(pimList!=null){
                try {
                    pimList.close();
                } catch (PIMException ex) {
                    ex.printStackTrace();
                }
            }
    Code:
    private PIMItem[] convertBytesToItems(InputStream is){
            PIMItem[] items = null;
            try {
                items = PIM.getInstance().fromSerialFormat(is, "UTF-8");
            } catch (Exception ex) {
            }
            return items;
        }
    nice and dandy - evenutally better rte exceptions handling is required,
    results are as expected, three items are added to contacts:

    results from S60 3rd Ed emulator
    Name
    Blazejewicz Peter
    Company
    Peter Blazejewicz
    Job title
    Junior Developer
    Telephone
    +48600306###
    E-mail
    peterblaze@######.pl
    UID
    12

    Name
    Junior Juarez
    E-mail
    juarez.barbosa@######.com
    UID
    13

    Name
    NKnair Balagopal
    E-mail
    balagopalks@######.com
    UID
    14
    from S40 3rdEd Fp1 EMulator (with world languages pack installed):
    1
    Opis
    Blazejewicz Peter
    Numer telefonu
    +48600306###
    Adres e-mail
    peterblaze@######.pl
    Notatka
    ########
    Warsaw 01-#####
    Poland
    Firma
    Peter Blazejewicz
    Stanowisko
    Junior Developer

    2
    Opis
    Junior Juarez
    Adres e-mail
    juarez.barbosa@######.com

    3
    Opis
    NKnair Balagopal
    Adres e-mail
    balagopalks@#####.com
    (### denotes sensitive data replaced by me),

    so I think that writing single vCards on server side into output stream is something we certainly can do without any issues, just write files into output writer and use a code like above one on client to parse data from http input stream,

    hth,
    regards,
    Peter

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    2

    Re: PIM Api and fromSerialFormat()

    Well i get a out Of memory Exception if i try to read a byte[] with around 150 to 200 conacts as vcf in. Any ideas? Whats eating all resources. I tried in a SonyEriscsson w810i...

  6. #6
    Super Contributor
    Join Date
    Dec 2005
    Location
    Europe/Poland/Warsaw
    Posts
    1,697

    Re: PIM Api and fromSerialFormat()

    hi,

    it could be that memory is not maintained well,
    remove all references of already created objects,
    try to calling System.gc(),
    etc,

    do you get that exception while input source is created or during convertion?

    regards,
    Peter

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    2

    Re: PIM Api and fromSerialFormat()

    Hi, Thanks!

    I found what was eating all resources, the follwing line:

    items.ensureCapacity(items.capacity()+newItems.length);

    It seems to be better to just create a Vector with a high initial capacity.

    Another issue: I have exported some vcards, now when reading them back i have trouble read the following,

    BEGIN:VCARD
    VERSION:2.1
    N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:;=C3=84=C3=B6=C3=A5;;;
    TEL;CELL;PREF:08666
    END:VCARD

    any ideas how to solve this?

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    1

    Smile Re: PIM Api and fromSerialFormat()

    Hi everybody,im new to j2me and im developing a MIDlet to retrieve the VCards,which is stored in the remote database.i finishe the MIDlet side well. and i want to send the Contacts using Java Servlet technology.an anyone direct me to write Serverside Code


    thanks in Advance

    R.Theesan
    theesan4u@hotmail.com

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    4,105

  10. #10
    Regular Contributor
    Join Date
    Oct 2008
    Posts
    121

    Re: PIM Api and fromSerialFormat()

    Quote Originally Posted by peterblazejewicz View Post
    Hi Divya,

    taken from jsr documentation:

    for me it sounds like that:
    - one can provide multile vCards in input stream
    - before first call of that method input stream is positioned at "begin" tag start
    - calling that method reads bytes from "begin" up to first "end" tag and leaves input stream open
    - method returns one or more PIM items (contact(s), event(s), todo(s)
    - we can call that method again on still opened input stream until end of input stream is achieved or exception occurs,
    hope that someone will clarify it with more details,
    hth,
    regards,
    peter
    Hi,

    As per PIM API

    Code:
    PIMItem[] items = PIM.getInstance().fromSerialFormat(inputStream, "UTF-8");
    This should return mutiple items. But as per documentation it would return first item between begin and end tag. And between BEGIN and END tags i think we can have only one PIM Item right. Please correct if this understanding is fine. Then the API should have been not returing PIMItem array. It should have returned single PIM Item.

    Now we have to manually handle the input stream by either parsing it. I was thinking if we could have used inputStream (Which contains multiple contacts)at one go.

    When we use loop

    Code:
    while (is.available() > 0 ){
       PIMItem[] items = PIM.getInstance().fromSerialFormat(inputStream, "UTF-8");
       // Above statement is throwing exception when it has read all the contacts
       // but the stream is still available. The end character is "ÿ".     
       if (items.length > 0) {
           // items.length is returning 1 only here
           // inputStream contains more than one contact in V-Card format
       }
       if(is.available()>=2)
       is.skip(2);
    }
    Handling exception and showing a success message might not be a good idea.

    Please guide.

    Thanks,
    Sandeep

  11. #11
    Regular Contributor
    Join Date
    Oct 2008
    Posts
    121

    Re: PIM Api and fromSerialFormat()

    Hi,

    Issue got resolved. Just for other's reference. I had to use ByteArrayInputStream instead of InputStream. Then bis.available was working fine.

    But i hope i future PIM API is added functionality that returns all PIM Items at one go in PIMItem [].

    Thanks,
    Sandeep

Similar Threads

  1. Reading vCards with the PIM API (JSR 75)
    By yoadg in forum Mobile Java General
    Replies: 7
    Last Post: 2006-08-03, 00:12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×