访问某个xml的url并将网页转成字节流,然后将字节流解析成document

 

  1. package domain;  
  2.  
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5.  
  6. import javax.microedition.io.Connector;  
  7. import javax.microedition.io.HttpConnection;  
  8.  
  9. import net.rim.device.api.io.Base64OutputStream;  
  10. import net.rim.device.api.xml.parsers.DocumentBuilder;  
  11. import net.rim.device.api.xml.parsers.DocumentBuilderFactory;  
  12.  
  13. import org.w3c.dom.Document;  
  14.  
  15. import exception.BBHttpConnectionFailedException;  
  16.  
  17. public class BBConnection {  
  18.         public InputStream inpConnectAndGetStream(String URL,String info)  
  19.                         throws BBHttpConnectionFailedException {  
  20.                 HttpConnection conn = null;  
  21.                 InputStream in;  
  22.                 try {  
  23.                         conn = (HttpConnection) Connector.open(URL + ";ConnectionTimeout=100000", Connector.READ_WRITE);  
  24.                         conn.setRequestMethod(HttpConnection.GET);  
  25.                         byte[] encoded = Base64OutputStream.encode(info.getBytes(),  
  26.                                         0, info.length(), falsefalse);  
  27.                         conn.setRequestProperty("Authorization""Basic " + new String(encoded));  
  28.                         in = conn.openDataInputStream();  
  29.                 } catch (Exception e) {  
  30.                         throw new BBHttpConnectionFailedException();  
  31.                 } finally {  
  32.                         if (conn != null) {  
  33.                                 try {  
  34.                                         conn.close();  
  35.                                 } catch (IOException e) {  
  36.                                         e.printStackTrace();  
  37.                                 }  
  38.                         }  
  39.                 }  
  40.                 return in;  
  41.         }  
  42.  
  43.         public Document docInputStreamtoDoc(InputStream in)  
  44.                         throws BBHttpConnectionFailedException {  
  45.                 Document document;  
  46.                 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
  47.                 try {  
  48.                         DocumentBuilder builder = factory.newDocumentBuilder();  
  49.                         document = builder.parse(in);  
  50.                 } catch (Exception e) {  
  51.                         throw new BBHttpConnectionFailedException();  
  52.                 }  
  53.                 return document;  
  54.         }  
  55.  
  56. }