well I am working on a yugioh game. I tried to get data from api and put it to database (sql server). The issue is when I use map.readTRee() to convert the data to json , It puts ? instead of the real caractere. What is strange is when I read the data stream threw BufferedReader System.out.printlen() read it correctly.
public class ApiEngine {
/**
* This function will collect all cards data from 'db.ygoprodeck.com/api/v7/cardinfo.php?'
*@return the result of API research that is a JSonNode type, null otherwise
*
*/
public static JsonNode getCardData()
{
try
{
URL url = new URL("https://db.ygoprodeck.com/api/v7/cardinfo.php?name=astral kuriboh");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
//Check if connect is made
int responseCode = conn.getResponseCode();
if (responseCode != 200)
{
throw new RuntimeException("HttpResponseCode: " + responseCode);
}
else
{
BufferedReader br = new BufferedReader(new InputStreamReader( url.openStream(), StandardCharsets.UTF_8));
/*String strCurrentLine;
while ((strCurrentLine = br.readLine()) != null) {
System.out.println(strCurrentLine);
}*/
ObjectMapper mapper = new ObjectMapper();
JsonNode cardData = mapper.readTree(br);
return cardData;
}
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
}
public class ExporterCardDataEngine {
/**
* This function will export API data to database
* @throws SQLException
*/
public static void exportApiData() throws SQLException
{
JsonNode cards = ApiEngine.getCardData().get("data");
Connection connexion = DatabaseEngine.connect();
int size = cards.size();
for (int i = 0; i < size; i++)
{
System.out.println((i + 1) +"/" + size + cards.get(i).get("desc"));
DatabaseEngine.insertCard(connexion, cards.get(i));
}
}
public static void main(String args[])
{
try {
ExporterCardDataEngine.exportApiData();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Here is the result when printing the bufferedReader
"desc":"You can reveal 1 \"Number\" Xyz Monster in your Extra Deck; Special Summon this card from your hand. If you do, this card's Level becomes the revealed monster's Rank, also while it is face-up in the Monster Zone, you cannot Special Summon monsters from the Extra Deck, except \"Number\" Xyz Monsters. You can only use this effect of \"Astral Kuriboh\" once per turn. A \"Number\" Xyz Monster that was Summoned using this card on the field as material gains this effect.\r\n\u25cf Cannot be destroyed by battle or your opponent's card effects."
Here after the mapper.readTree()
1/1"You can reveal 1 \"Number\" Xyz Monster in your Extra Deck; Special Summon this card from your hand. If you do, this card's Level becomes the revealed monster's Rank, also while it is face-up in the Monster Zone, you cannot Special Summon monsters from the Extra Deck, except \"Number\" Xyz Monsters. You can only use this effect of \"Astral Kuriboh\" once per turn. A \"Number\" Xyz Monster that was Summoned using this card on the field as material gains this effect.\r\n? Cannot be destroyed by battle or your opponent's card effects."
thank you for your help