I tried to make something like an EC site using Yahoo's API, but I fell into a situation where I didn't know how to use it at all. I didn't understand it even if I looked it up, and it was quite difficult, so I will record it including the meaning of the memo.
By the way, I have been learning Java for about 3 months.
Apparently, the way to send a request to the API is to connect to the URL, There was a class for that. That is the ** HttpURLConnection class **.
The basic steps are
It's like that.
-Face with URLConnection (HttpURLConnection) ~ Get data with GET method ~
//Application ID
String appid = "Application ID";
//Receives product keywords and stores them in String type
request.setCharacterEncoding("UTF-8");
String query = request.getParameter("searchVal");
//Request URL
String url = "https://shopping.yahooapis.jp/ShoppingWebService/V1/json/itemSearch?appid="+appid+"&query="+query;
//URL generation for API connection
URL url4conn = new URL(url);
//Get a connection to the URL for the API connection
HttpURLConnection conn = (HttpURLConnection)url4conn.openConnection();
//Specify HTTP method for GET
conn.setRequestMethod("GET");
//Do not allow body submission of request
conn.setDoOutput(false);
//Allow body transmission of response
conn.setDoInput(true);
//Connect
conn.connect();
Apparently, to get the response from the API, you can read the response using BufferedReader. However, since you can get ** JASON character string ** at the stage of reading, you need to convert it to ** JASON node (type for JSON) ** using the library.
The basic steps are
It's like that.
//Read response(Get JASON string)
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String json = br.readLine();
//Read JSON string and convert to JsonNode object
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(json);
The basic steps are
It's like that.
Use ** JsonNode.get () ** to retrieve a specific element from a JSON node.
--Read data with JSON library Jackson for Java
//Generate an ArrayList that stores Beans containing product search results
ArrayList<productDataBeans> pdbList = new ArrayList<productDataBeans>();
//Extract 10 elements from JSON and store in String type
//Set it in Beans and store Beans in ArrayList
for(int i = 0; i <= 9; i++) {
String hitNum = String.valueOf(i);
String imageURL = root.get("ResultSet").get("0").get("Result").get(hitNum).get("Image").get("Small").textValue();
String productName = root.get("ResultSet").get("0").get("Result").get(hitNum).get("Name").textValue();
String price = root.get("ResultSet").get("0").get("Result").get(hitNum).get("Price").get("_value").textValue();
int SearchResultNum = root.get("ResultSet").get("totalResultsAvailable").asInt();
//Create an instance of Beans to store product search results
productDataBeans pdb = new productDataBeans();
pdb.setImageURL(imageURL);
pdb.setProductName(productName);
pdb.setPrice(price);
pdb.setQuery(query);
pdb.setSearchResultNum(SearchResultNum);
pdbList.add(pdb);
}
//Store ArrayList in request scope
request.setAttribute("resultData", pdbList);
br.close();
request.getRequestDispatcher("/search.jsp").forward(request, response);
I mentioned that you need to convert the JSON string you read into a JSON node, but in the case of Jackson, you need three libraries.
・ Jackson-core -Jackson-annotations ・ Jackson-databind
You can download it by selecting the linked version and pressing the bundle button.
Recommended Posts