gpt4 book ai didi

android - android 中的 Api rest 调用不起作用

转载 作者:行者123 更新时间:2023-11-30 03:03:59 25 4
gpt4 key购买 nike

我正在尝试按如下方式进行 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

最佳答案

任何网络操作都应该在 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();

关于android - android 中的 Api rest 调用不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22124787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com