Вопрос

I'm trying to make an api rest call as following:

InputSource is = new InputSource("http://api.eventful.com/rest/events/search?app_key=5mnzXGn4S4WsNxKS&keywords=books&location=paris&date=Future");
        is.setEncoding("ISO-8859-1");
ParseurEvent parseur = new ParseurEvent(is);

my event parser:

List<Event> eventList;
        InputSource bookXmlFileName;
        String tmpValue;
        Event eventTmp;
        //SimpleDateFormat sdf= new SimpleDateFormat("yy-MM-dd");


        //Constructor
        public ParseurEvent(InputSource bookXmlFileName) {
            this.bookXmlFileName = bookXmlFileName;
            eventList = new ArrayList<Event>();
            parseDocument();
            printDatas();
        }


        private void parseDocument() {
            // parse
            SAXParserFactory factory = SAXParserFactory.newInstance();
            try {
                SAXParser parser = factory.newSAXParser();
                parser.parse(bookXmlFileName, this);
            } catch (ParserConfigurationException e) {
                Log.e("myParserConfigurationException", "ParserConfig error");
            } catch (SAXException e) {
                Log.e("mySAXException", "SAXException : xml not well formed");
            } catch (IOException e) {
                Log.e("myIOException", e.getMessage());         
            }
        }
        public List<Event> printDatas() {
            for (Event event : eventList) {
                Log.i("Event", event.getTitle());               
            }

            return eventList;           
        }
        @Override
        public void startElement(String s, String s1, String elementName, Attributes attributes) throws SAXException {
            if (elementName.equalsIgnoreCase("event")) {
                eventTmp = new Event();
                eventTmp.setId(attributes.getValue("id"));
            }           
        }
        @Override
        public void endElement(String s, String s1, String element) throws SAXException {
            // if end of book element add to list
            if (element.equals("event")) {
                eventList.add(eventTmp);
            }
            if (element.equalsIgnoreCase("title")) {
                eventTmp.setTitle(tmpValue);
            }
            if (element.equalsIgnoreCase("url")) {
                eventTmp.setUrl(tmpValue);
            }
            if (element.equalsIgnoreCase("description")) {
                eventTmp.setDescription(tmpValue);
            }
            if(element.equalsIgnoreCase("start_time")){
                eventTmp.setStart_time(tmpValue);
            } 
        }
        @Override
        public void characters(char[] ac, int i, int j) throws SAXException {
            tmpValue = new String(ac, i, j);
        }
}

I get this exception:

Couldn't open http://api.eventful.com/rest/events/search?app_key=5mnzXGn4S4WsNxKS&keywords=books&location=paris&date=Future

Это было полезно?

Решение

Any network operation should be done in an AsyncTask. And url.openStream should be helpful in this case.

class ParseTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... n) {
        try {
            URL url = new URL("http://api.eventful.com/rest/events/search?app_key=5mnzXGn4S4WsNxKS&keywords=books&location=paris&date=Future");
            InputStream is = url.openStream();
            is.setEncoding("ISO-8859-1");

            ParseurEvent parseur = new ParseurEvent(is);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}

You can call this task as:

new ParseTask().execute();

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top