访问某个xml的url并将网页转成字节流,然后将字节流解析成document
- package domain;
- import java.io.IOException;
- import java.io.InputStream;
- import javax.microedition.io.Connector;
- import javax.microedition.io.HttpConnection;
- import net.rim.device.api.io.Base64OutputStream;
- import net.rim.device.api.xml.parsers.DocumentBuilder;
- import net.rim.device.api.xml.parsers.DocumentBuilderFactory;
- import org.w3c.dom.Document;
- import exception.BBHttpConnectionFailedException;
- public class BBConnection {
- public InputStream inpConnectAndGetStream(String URL,String info)
- throws BBHttpConnectionFailedException {
- HttpConnection conn = null;
- InputStream in;
- try {
- conn = (HttpConnection) Connector.open(URL + ";ConnectionTimeout=100000", Connector.READ_WRITE);
- conn.setRequestMethod(HttpConnection.GET);
- byte[] encoded = Base64OutputStream.encode(info.getBytes(),
- 0, info.length(), false, false);
- conn.setRequestProperty("Authorization", "Basic " + new String(encoded));
- in = conn.openDataInputStream();
- } catch (Exception e) {
- throw new BBHttpConnectionFailedException();
- } finally {
- if (conn != null) {
- try {
- conn.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return in;
- }
- public Document docInputStreamtoDoc(InputStream in)
- throws BBHttpConnectionFailedException {
- Document document;
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- try {
- DocumentBuilder builder = factory.newDocumentBuilder();
- document = builder.parse(in);
- } catch (Exception e) {
- throw new BBHttpConnectionFailedException();
- }
- return document;
- }
- }