2012-04-28 15 views
2

我在j2me中有一個從wml/asp頁面獲取字符串和數據的程序。
使用此代碼:使用J2me從WML頁面獲取數據

HttpConnection con = (HttpConnection) Connector.open(
    "http://localhost:"+port+"/MobileWebWIthConnection/ShowCourseinsemester.aspx?StudentId="+ID+"&Year="+Year+"&Semester="+Semester); 
DataInputStream in = new DataInputStrea(con.openInputStream()); 
int len = (int) con.getLength(); 
byte[] info = new byte[len]; 
in.readFully(info); 
result = new String(info); 

switchDisplayable(null, getStudentCourses()); 
stringItem2.setText(result); 

當我的J2ME應用程序試圖從該頁面讀取和存儲數據:

"http://localhost:"+port+"/MobileWebWIthConnection/ShowCourseinsemester.aspx?StudentId="+ID+"&Year="+Year+"&Semester="+Semester 

其放置在名爲(結果)字符串中的文字是什麼相似下面預期數字:

The Page called

它走的是內容,而無需如下格式:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 



<wml> 
<card> 
<p><b>Student Name :</b> Arin     Rizk    </p> 
<p><b>Student ID</b> : 20111</p> 
<p>first Semester ,2011</p> 
1 - Course Name : DDD  | Credits Number : 3   | Mark : 70   </br> 2 - Course Name : EEE  | Credits Number : 3   | Mark : 65   </br> 3 - Course Name : EEE  | Credits Number : 3   | Mark : 65   </br> 4 - Course Name : EEE  | Credits Number : 3   | Mark : 90   </br> 
</card> 
</wml> 

所以當我分配這個文本它顯示在圖下面的StringItem的。

stringItem2.setText(result); 

enter image description here

怎樣才能讓我的J2ME查看字符串作爲原始格式的頁面?

回答

1

我解決了它,這是有點棘手,特別是在j2me沒有(拆分方法)。

所以我簡單地創建了一個。

我decleared它

String[] split (String x){ 
     int num=0; 
     for(int i=0; i<x.length(); i++) // count the number of ',' 
      if(x.charAt(i)==',') 
       num++; 

     String[] r=new String[num]; 
     for(int i=0; i<num; i++) 
     { 
      int loc=x.indexOf(","); //loc is the location of each ',' 
      r[i]=x.substring(0,loc); 
      x=x.substring(loc+1); 
     } 
      return r; 
     } 

,然後我申請它,並顯示在列表

HttpConnection con = (HttpConnection) Connector.open("http://localhost:"+port+"/MobileWebWIthConnection/ShowCourseinsemester.aspx?StudentId="+ID+"&Year="+Year+"&Semester="+Semester); 
          DataInputStream in = new DataInputStream(con.openInputStream()); 
          int len = (int) con.getLength(); 
          byte[] info = new byte[len]; 
          in.read(info); 
          result = new String(info);       
          String[] a=split(result); 
        getList().deleteAll(); 
        for(int i=1; i<a.length; i++) 
         getList().append(a[i], null); 

        switchDisplayable(null,getList()); 

結果並作爲想要的結果(以行),而不從所述完整的源代碼wml頁面。

enter image description here