2013-04-10 28 views
0

我正在閱讀使用此代碼的服務器的響應。如何在黑莓的對象選擇字段中顯示HTTP響應

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

} 

我需要選擇對象領域顯示此實體。 例如: 假設我得到回覆實體= ThisIsGoingToGood

然後,我需要在對象選擇下拉列表中顯示下面的方式。

請告訴我如何做到這一點。

+0

你會喜歡「ThisIsGoingToGood」。然後通過分割「This」,「Is」,「Going」,「To」,「Good」等文字來轉換。你是什​​麼意思 ? – Signare 2013-04-10 07:22:32

+0

@ Signare-它不是第一個文本文件。它是在代碼中顯示的實體字符串中的響應。通過分割實體字符串的文本。我試圖展示我如何在對象選擇領域需要這個。作爲「這個」應該是choicefield的第一選擇。「是」應該是choicefield的第二選擇等等。 – user2218773 2013-04-10 07:24:04

+0

**你在問什麼**問題?你問如何拆分一個沒有空格的字符串?你問的是如何從後臺線程更新ObjectListField,這是請求HTTP數據? – Nate 2013-04-10 08:13:15

回答

0

該解決方案假定:

  • 你的字符串的駝峯格式都是開始用大寫字母。

  • 即使單詞是首字母縮寫詞,也只使用一行中的一個大寫字母。例如,「HTTP響應」將被寫爲"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; 
} 

然後,你就會有一個很好的Vector充分的選擇,爲您的ObjectChoiceField。那麼你可以insert()他們,如Signare的答案所示。

注意:總是記得關閉你的流。不過,我已經把它交給你來決定你什麼時候完成了。

+0

謝謝,它爲我工作。 – user2218773 2013-04-10 12:35:07

0

與Nate的答案 - 裁判試試這個 -

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

請告訴我如何從「ThisISGoingToGood」獲取尺寸和價值。 – user2218773 2013-04-10 09:38:08

相關問題