您的位置:首页 > 移动开发 > Android开发

android解析服务器发过来的XML代码

2013-05-02 16:14 288 查看
//连接服务器

    public static InputStream openConn(String path) {

        HttpURLConnection uc;

        URL url;

        InputStream is = null;

        try {

            url = new URL(path);

            // SocketAddress addr = new

            // InetSocketAddress("10.0.0.172",80);//是代理地址

            // Proxy typeProxy = new Proxy(Proxy.Type.HTTP, addr);

            // uc = (HttpURLConnection) url.openConnection(typeProxy);

            uc = (HttpURLConnection) url.openConnection();

            uc.connect();

            is = uc.getInputStream();

            //uc.disconnect();

        } catch (MalformedURLException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        

        return is;

    }

//XML解析部分

        InputStream is = openConn(你要解析的服务器的页面);

        

        //无法连接

        if (is == null)

            return false;

        

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        DocumentBuilder builder = null;

        try {

            builder = factory.newDocumentBuilder();

        } catch (ParserConfigurationException e1) {

            // TODO Auto-generated catch block

            e1.printStackTrace();

   
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: