Saturday, December 1, 2012

Https Url parsing Data

public static String[] getSongData(String url) {
        String[] mTitle = new String[2];
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(OpenHttpsConnection(url));
            doc.getDocumentElement().normalize();
            NodeList nodeList = doc.getElementsByTagName("NowPlaying");
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                Element fstElmnt = (Element) node;
                NodeList nameList = fstElmnt.getElementsByTagName("Title");
                Element nameElement = (Element) nameList.item(0);
                nameList = nameElement.getChildNodes();
                mTitle[0] = ((Node) nameList.item(0)).getNodeValue();
                NodeList websiteList = fstElmnt.getElementsByTagName("Artist");
                Element websiteElement = (Element) websiteList.item(0);
                websiteList = websiteElement.getChildNodes();
                mTitle[1] = ((Node) websiteList.item(0)).getNodeValue();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return mTitle;
    }

    public static InputStream OpenHttpsConnection(String strURL)
            throws IOException, NoSuchAlgorithmException,
            KeyManagementException {
        InputStream inputStream = null;
        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new X509TrustManager[] { new X509TrustManager() {
            public void checkClientTrusted(X509Certificate[] chain,
                    String authType) throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] chain,
                    String authType) throws CertificateException {
            }

            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
        } }, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(context
                .getSocketFactory());

        URL url = new URL(strURL);
        URLConnection conn = url.openConnection();
        try {
            HttpsURLConnection httpConn = (HttpsURLConnection) conn;
            httpConn.setRequestMethod("GET");
            httpConn.connect();
            if (httpConn.getResponseCode() == HttpsURLConnection.HTTP_OK) {
                inputStream = httpConn.getInputStream();
            }
        } catch (Exception ex) {
        }
        return inputStream;
    }

No comments:

Post a Comment