在Android中的Api休息调用不起作用

时间:2014-03-02 06:53:58

标签: android rest

我正在尝试进行api休息呼叫,如下所示:

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);

我的事件解析器:

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);
        }
}

我得到了这个例外:

无法打开http://api.eventful.com/rest/events/search?app_key=5mnzXGn4S4WsNxKS&keywords=books&location=paris&date=Future

1 个答案:

答案 0 :(得分:1)

任何网络操作都应该在AsyncTask中完成。在这种情况下,url.openStream应该会有所帮助。

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;
    }


}

您可以将此任务称为:

new ParseTask().execute();