gpt4 book ai didi

java - JAXB - 从 url 解码

转载 作者:数据小太阳 更新时间:2023-10-29 01:59:23 33 4
gpt4 key购买 nike

我正在尝试显示来自该站点的游戏的标题和 ID: http://thegamesdb.net/api/GetGame.php?id=2

当我从这个 url 解码时:http://www.w3schools.com/xml/note.xml一切都很好,但这里只有一个对象,而不是列表。所以我现在有问题。我正在阅读 Google 的一些教程和示例,并编写了以下代码:

数据.java:

@XmlRootElement( name = "Data" )
@XmlAccessorType (XmlAccessType.FIELD)
public class Data {
@XmlElement(name = "Game")
List<Game> games;

public List<Game> getGames() {
return games;
}

public void setGames(List<Game> games) {
this.games = games;
}
}

游戏.java文件:

@XmlRootElement(name = "Game")
@XmlAccessorType (XmlAccessType.FIELD)
public class Game {
private int id;
private String gameTitle;

public int getId(){
return id;
}

public void setId(int id){
this.id = id;
}

public String getGameTitle(){
return gameTitle;
}

public void setGameTitle(String gameTitle){
this.gameTitle = gameTitle;
}
}

Controller :

@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView home(Locale locale) throws MalformedURLException {
ModelAndView model = new ModelAndView("index");
JAXBExample test = new JAXBExample();
Game customer = test.readXML();
model.addObject("customer", customer);
return model;
}

JAXBExample.java:

public class JAXBExample {
public Game readXML() throws MalformedURLException {
Data customer = null;
Game game = null;
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Data.class);
URL url = new URL("http://thegamesdb.net/api/GetGame.php?id=2");
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
customer = (Data) jaxbUnmarshaller.unmarshal(url);
List<Game> games = customer.getGames();
game = games.get(0);
} catch (JAXBException e) {
e.printStackTrace();
}
return game;
}
}

和index.jsp:

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>

<tiles:insertDefinition name="defaultTemplate">
<tiles:putAttribute name="body">
Name: ${customer.gameTitle}<br />
Id: ${customer.id}<br />
</tiles:putAttribute>
</tiles:insertDefinition>

但是我的代码不起作用。有人可能知道我做错了什么?因为结果我得到:

Name:
Id:

仅此而已。

最佳答案

你的注释唯一有问题的是

public class Game {
private int id;
@XmlElement(name = "GameTitle") //You need to add this since first letter is uppercase, otherwise the GameTitle will not unmarshall.
private String gameTitle;
... your code ...
}

那么为什么其余部分不起作用?

Server returned HTTP response code: 403 for URL: http://thegamesdb.net/api/GetGame.php?id=2

403 = Forbidden

解决方案(让服务器相信你是浏览器)

URL url = new URL("http://thegamesdb.net/api/GetGame.php?id=2");
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.addRequestProperty("User-Agent", "Mozilla/4.76");
InputStream is = http.getInputStream();
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
customer = (Data) jaxbUnmarshaller.unmarshal(is);
List<Game> games = customer.getGames();
game = games.get(0);

注意:Try catch final、关闭流和检查 NullPointer 超出了这个示例,由用户决定。

关于java - JAXB - 从 url 解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34278493/

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