2014-03-02 66 views
1

我試圖做一個API REST調用如下:阿比休息不能正常工作

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

任何網絡操作都應該在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();

+0

因此,真正:)非常感謝 –