Я пытался загрузить изображения из файла JSON с помощью GSON, и после прочтения нескольких потоков в StackOverflow я обнаружил, что лучшим решением было сохранить URL-адрес в файле JSON и затем загрузить его в изображение. Проблема в том, что мое приложение не может загрузить эти URL-адреса в изображение, и я понятия не имею, почему. Например, когда я вхожу в режим отладки: Изображение режима отладки
Показывает, что изображения нулевые.
Вот мой файл JSON:
{
"shoes": [
{
"shoeName": "Shoe",
"shoePrice": "120",
"brand": "Shoe",
"typeOfShoes": "Running",
"style": "Cool",
"colors": [
"Blue",
"Green",
"Pink"
],
"sizes": [
"W5/M3.5",
"W5.5/M4"
],
"description": "The Shoe SE features sleek lines and a sheer upper that combine classic Air Max elements into a lightweight, comfortable and versatile icon. Together with its smart toe-down profile and extra lift, the shoe offers an ever-bigger expression..",
"shipping": "0",
"tax": "0",
"subtotal": "0",
"review": "4.5",
"totalRaffles": "80",
"imageURLs": ["https://www.nike.com/t/vaporfly-4-flyknit-running-shoe-v7G3FB"],
"isSold": "false"
},
{
"shoeName": "Empty Shoe",
"shoePrice": "0",
"brand": "null",
"typeOfShoes": "null",
"style": "null",
"colors": ["null"
],
"sizes": ["null"
],
"description": "null",
"shipping": "0",
"tax": "0",
"subtotal": "0",
"review": "0",
"totalRaffles": "0",
"imageURLs": ["https://www.nike.com/t/vaporfly-4-flyknit-running-shoe-v7G3FB"],
"isSold": "false"
},
{
"shoeName": "Empty Shoe1",
"shoePrice": "0",
"brand": "null",
"typeOfShoes": "null",
"style": "null",
"colors": ["null"
],
"sizes": ["null"
],
"description": "null",
"shipping": "0",
"tax": "0",
"subtotal": "0",
"review": "0",
"totalRaffles": "0",
"imageURLs": ["https://www.nike.com/t/vaporfly-4-flyknit-running-shoe-v7G3FB"],
"isSold": "false"
}
]
}
ReadJSON.java - это класс для чтения файла JSON.
ReadJSON.java
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pulsebeat02.main.gui.Shoe;
import com.pulsebeat02.main.gui.windows.StartingWindow;
public class ReadJSON {
public static void main(String[] args) {
// TODO Auto-generated method stub
parseJSON();
}
public static void parseJSON() {
String cwd = System.getProperty("user.dir");
File f = new File(cwd + "/shoes.json");
String str = null;
try {
str = readFile(f);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Gson gson = new Gson();
Type dataType = (new TypeToken<Shoes>() {
}).getType();
Shoes shoeList = gson.fromJson(str, dataType);
List<Shoe> shoes = shoeList.getShoes();
Shoe [] shoesFinal = new Shoe[shoes.size()];
for (int i = 0; i < shoesFinal.length; i++) {
shoesFinal[i] = shoes.get(i);
}
StartingWindow.shoesInGui = shoesFinal;
System.out.println(shoeList);
System.out.println("Ran");
}
public static String readFile(File file) throws IOException {
return new String(Files.readAllBytes(file.toPath()));
}
}
Вот класс Shoe, используемый для определения объекта обуви:
Shoe.java
import java.awt.Image;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import javax.imageio.ImageIO;
public class Shoe {
public int shoePrice;
public int shipping;
public int tax;
public int subtotal;
public int totalRaffles;
public double review;
public String shoeName;
public String style;
public String typeOfShoes;
public String brand;
public String[] imageURLs;
public Image[] images;
public String description;
public String[] colors;
public String[] sizes;
public boolean isSold;
public Shoe(int shoePrice, int shipping, int tax, int subtotal, double review,
int totalRaffles,
String shoeName, String style, String typeOfShoes, String brand,
String[] imageURLs,
String description, String[] colors, String[] sizes,
boolean isSold) {
this.shoePrice = shoePrice;
this.shipping = shipping;
this.tax = tax;
this.subtotal = subtotal;
this.review = review;
this.totalRaffles = totalRaffles;
this.sizes = sizes;
this.shoeName = shoeName;
this.style = style;
this.typeOfShoes = typeOfShoes;
Image[] imagesFinal = new Image[imageURLs.length];
for (int i = 0; i < imageURLs.length; i++) {
URL url = null;
try {
url = new URL(imageURLs[i]);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Image c = null;
try {
c = ImageIO.read(url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
imagesFinal[i] = c;
}
this.images = imagesFinal;
this.description = description;
this.colors = colors;
this.isSold = isSold;
this.brand = brand;
}
public Shoe(String shoePrice2, String shipping2, String tax2, String subTotal2, String review2,
String totalRaffles2, String shoeName2, String style2, String typeOfShoes2, String brand2,
List<String> images2, String description2, List<String> color, List<String> sizes2, String isSold2) {
this.shoePrice = Integer.parseInt(shoePrice2);
this.shipping = Integer.parseInt(shipping2);
this.tax = Integer.parseInt(tax2);
this.subtotal = Integer.parseInt(subTotal2);
this.review = Double.parseDouble(review2);
this.totalRaffles = Integer.parseInt(totalRaffles2);
this.shoeName = shoeName2;
this.style = style2;
this.typeOfShoes = typeOfShoes2;
this.brand = brand2;
this.images = loadImage((String[]) images2.toArray());
this.description = description2;
this.colors = (String[]) color.toArray();
this.sizes = (String[]) sizes2.toArray();
this.isSold = Boolean.getBoolean(isSold2);
}
public Shoe (Shoe s) {
super();
}
public static Shoe[] toArray(List<Shoe> list) {
Shoe[] shoes = new Shoe[list.size()];
for (int i = 0; i < list.size(); i++) {
shoes[i] = list.get(i);
}
return shoes;
}
public static Image[] loadImage(String[] imageURLs) {
Image[] images = new Image[imageURLs.length];
for (int i = 0; i < imageURLs.length; i++) {
Image image = null;
try {
image = ImageIO.read(new URL(imageURLs[i]));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
images[i] = image;
}
return images;
}
}