Question

I am reading a response from server using this code.

public static String getContentString(HttpConnection Connection) throws IOException
{
    String Entity=null;
    InputStream inputStream;
    inputStream = Connection.openInputStream();//May give network io
    StringBuffer buf = new StringBuffer();
    int c;
    while ((c = inputStream.read()) != -1) 
    {
         buf.append((char) c);
    }

    //Response Formation            
    try
    {
        Entity = buf.toString();
        return Entity;
    }
    catch(NullPointerException e)
    {
        Entity = null;
        return Entity;
    }

}

I need to show this Entity in object choice field. For example: suppose i get response Entity=ThisIsGoingToGood

then, I need to show below way in object choice drop down list.

  • This

  • Is

  • Going

  • To

  • Good

Please tell me how to achieve this.

Was it helpful?

Solution

This solution assumes:

  • The Camel Case format of your strings will always start with an upper case letter.

  • Only one upper case character in a row is used, even if the word is an acronym. For example, "HTTP response" would be written as "HttpResponse".

public static Vector getContentStrings(HttpConnection connection) throws IOException {
    Vector words = new Vector();

    InputStream inputStream = connection.openInputStream();
    StringBuffer buf = new StringBuffer();
    int c;
    while ((c = inputStream.read()) != -1) 
    {
        char character = (char)c;
        if (CharacterUtilities.isUpperCase(character)) {
            // upper case -> new word
            if (buf.length() > 0) {
                words.addElement(buf.toString());
                buf = new StringBuffer();
            }
        }
        buf.append(character);
    }
    // add the last word
    words.addElement(buf.toString());

    return words;
}

Then, you'll have a nice Vector full of the choices for your ObjectChoiceField. You can then insert() them, as shown in Signare's answer.

Note: always remember to close your streams, too. I've leave it to you to decide when you're really finished with it, though.

OTHER TIPS

With ref from Nate's Answer- try this -

ObjectListField ol = new ObjectListField(ObjectListField.ELLIPSIS);
ol.setSize(words.size()); //Where words is the vector 
for (int i = 0; i < size; i++)
    {
ol.insert(i, words.elementAt(i));
}
add(ol);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top