Search in sources :

Example 1 with NetcastChannelParser

use of com.connectsdk.service.netcast.NetcastChannelParser in project butter-android by butterproject.

the class NetcastTVService method getChannelList.

@Override
public void getChannelList(final ChannelListListener listener) {
    String requestURL = getUDAPRequestURL(UDAP_PATH_DATA, TARGET_CHANNEL_LIST);
    ResponseListener<Object> responseListener = new ResponseListener<Object>() {

        @Override
        public void onSuccess(Object response) {
            String strObj = (String) response;
            try {
                SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
                InputStream stream = new ByteArrayInputStream(strObj.getBytes("UTF-8"));
                SAXParser saxParser = saxParserFactory.newSAXParser();
                NetcastChannelParser parser = new NetcastChannelParser();
                saxParser.parse(stream, parser);
                JSONArray channelArray = parser.getJSONChannelArray();
                ArrayList<ChannelInfo> channelList = new ArrayList<ChannelInfo>();
                for (int i = 0; i < channelArray.length(); i++) {
                    JSONObject rawData;
                    try {
                        rawData = (JSONObject) channelArray.get(i);
                        ChannelInfo channel = NetcastChannelParser.parseRawChannelData(rawData);
                        channelList.add(channel);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                Util.postSuccess(listener, channelList);
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onError(ServiceCommandError error) {
            Util.postError(listener, error);
        }
    };
    ServiceCommand<ResponseListener<Object>> request = new ServiceCommand<ResponseListener<Object>>(this, requestURL, null, responseListener);
    request.setHttpMethod(ServiceCommand.TYPE_GET);
    request.send();
}
Also used : NetcastChannelParser(com.connectsdk.service.netcast.NetcastChannelParser) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) ChannelInfo(com.connectsdk.core.ChannelInfo) ServiceCommandError(com.connectsdk.service.command.ServiceCommandError) ResponseListener(com.connectsdk.service.capability.listeners.ResponseListener) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) JSONObject(org.json.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) SAXParser(javax.xml.parsers.SAXParser) JSONObject(org.json.JSONObject) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ServiceCommand(com.connectsdk.service.command.ServiceCommand) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 2 with NetcastChannelParser

use of com.connectsdk.service.netcast.NetcastChannelParser in project butter-android by butterproject.

the class NetcastTVService method getCurrentChannel.

@Override
public void getCurrentChannel(final ChannelListener listener) {
    String requestURL = getUDAPRequestURL(UDAP_PATH_DATA, TARGET_CURRENT_CHANNEL);
    ResponseListener<Object> responseListener = new ResponseListener<Object>() {

        @Override
        public void onSuccess(Object response) {
            String strObj = (String) response;
            try {
                SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
                InputStream stream = new ByteArrayInputStream(strObj.getBytes("UTF-8"));
                SAXParser saxParser = saxParserFactory.newSAXParser();
                NetcastChannelParser parser = new NetcastChannelParser();
                saxParser.parse(stream, parser);
                JSONArray channelArray = parser.getJSONChannelArray();
                if (channelArray.length() > 0) {
                    JSONObject rawData = (JSONObject) channelArray.get(0);
                    ChannelInfo channel = NetcastChannelParser.parseRawChannelData(rawData);
                    Util.postSuccess(listener, channel);
                }
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onError(ServiceCommandError error) {
            Util.postError(listener, error);
        }
    };
    ServiceCommand<ResponseListener<Object>> request = new ServiceCommand<ResponseListener<Object>>(this, requestURL, null, responseListener);
    request.send();
}
Also used : NetcastChannelParser(com.connectsdk.service.netcast.NetcastChannelParser) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) ChannelInfo(com.connectsdk.core.ChannelInfo) ServiceCommandError(com.connectsdk.service.command.ServiceCommandError) ResponseListener(com.connectsdk.service.capability.listeners.ResponseListener) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) JSONObject(org.json.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) SAXParser(javax.xml.parsers.SAXParser) JSONObject(org.json.JSONObject) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ServiceCommand(com.connectsdk.service.command.ServiceCommand) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Aggregations

ChannelInfo (com.connectsdk.core.ChannelInfo)2 ResponseListener (com.connectsdk.service.capability.listeners.ResponseListener)2 ServiceCommand (com.connectsdk.service.command.ServiceCommand)2 ServiceCommandError (com.connectsdk.service.command.ServiceCommandError)2 NetcastChannelParser (com.connectsdk.service.netcast.NetcastChannelParser)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 SAXParser (javax.xml.parsers.SAXParser)2 SAXParserFactory (javax.xml.parsers.SAXParserFactory)2 JSONArray (org.json.JSONArray)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 SAXException (org.xml.sax.SAXException)2 ArrayList (java.util.ArrayList)1