以下是一個使用Java編寫的簡單示例,演示如何通過API接口采集商品信息并接入到代購程序中:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.PrintWriter;
import java.net.URLConnection;public class Example {private static String readAll(Reader rd) throws IOException {StringBuilder sb = new StringBuilder();int cp;while ((cp = rd.read()) != -1) {sb.append((char) cp);}return sb.toString();}public static JSONObject postRequestFromUrl(String url, String body) throws IOException, JSONException {URL realUrl = new URL(url);URLConnection conn = realUrl.openConnection();conn.setDoOutput(true);conn.setDoInput(true);PrintWriter out = new PrintWriter(conn.getOutputStream());out.print(body);out.flush();InputStream instream = conn.getInputStream();try {BufferedReader rd = new BufferedReader(new InputStreamReader(instream, Charset.forName("UTF-8")));String jsonText = readAll(rd);JSONObject json = new JSONObject(jsonText);return json;} finally {instream.close();}}public static JSONObject getRequestFromUrl(String url) throws IOException, JSONException {URL realUrl = new URL(url);URLConnection conn = realUrl.openConnection();InputStream instream = conn.getInputStream();try {BufferedReader rd = new BufferedReader(new InputStreamReader(instream, Charset.forName("UTF-8")));String jsonText = readAll(rd);JSONObject json = new JSONObject(jsonText);return json;} finally {instream.close();}}public static void main(String[] args) throws IOException, JSONException {// 請求示例 url 默認請求參數已經URL編碼處理String url = "https://item_get/?key=<您自己的apiKey>&secret=<您自己的apiSecret>&num_iid=652874751412&is_promotion=1";JSONObject json = getRequestFromUrl(url);System.out.println(json.toString());}}
請注意,上述代碼僅為演示目的,實際應用中需要根據具體的API接口文檔和代購程序的要求進行相應的修改和適配。
響應參數
名稱 | 類型 | 必須 | 示例值 | 描述 |
---|---|---|---|---|
item | item[] | 1 | 寶貝詳情數據 | |
num_iid | Bigint | 1 | 520813250866 | 寶貝ID |
title | String | 1 | 三刃木折疊刀過安檢創意迷你鑰匙扣鑰匙刀軍刀隨身多功能小刀包郵 | 寶貝標題 |
desc_short | String | 0 | 商品簡介 | |
promotion_price | Int | 0 | 優惠價 | |
price | Float | 1 | 25.8 | 價格 |
total_price | Float | 0 | 0 | |
suggestive_price | Float | 0 | 0 | |
orginal_price | String | 0 | 25.80 | 原價 |
nick | String | 0 | 歡樂購客棧 | 掌柜昵稱 |
num | Int | 0 | 3836 | 庫存(沒有精確,是模糊值) |
min_num | Int | 0 | 0 | 最小購買數 |
detail_url | String | 0 | http://item.taobao.com/item.htm?id=520813250866 | 寶貝鏈接 |
pic_url | String | 1 | //gd2.alicdn.com/imgextra/i4/2596264565/TB2p30elFXXXXXQXpXXXXXXXXXX_!!2596264565.jpg | 寶貝圖片 |
brand | String | 0 | 三刃木 | 品牌名稱 |
brandId | Int | 0 | 8879363 | 品牌ID |
rootCatId | Int | 0 | 50013886 | 頂級分類ID |
cid | Int | 1 | 50014822 | |
crumbs | Mix | 0 | [] | 導航菜單 |
created_time | String | 0 | ||
modified_time | String | 0 | ||
delist_time | String | 0 | ||
desc | String | 0 | 商品詳情 | |
desc_img | Mix | 0 | [] | 商品詳情圖片 |
item_imgs | Mix | 0 | item_imgs[] | 商品圖片 |
url | string | 0 | //gd3.alicdn.com/imgextra/i4/2596264565/TB2p30elFXXXXXQXpXXXXXXXXXX_!!2596264565.jpg | |
item_weight | String | 0 | ||
item_size | String | 0 | ||
location | String | 0 | 發貨地 | |
express_fee | Float | 0 | 0.00 | 快遞費用 |
ems_fee | Float | 0 | EMS費用 | |
post_fee | Float | 0 | 物流費用 | |
shipping_to | String | 0 | 發貨至 | |
has_discount | Boolean | 0 | false | 是否有優惠 |
video | video[] | 0 | 商品視頻 | |
is_virtual | String | 0 | ||
is_promotion | Boolean | 0 | false | 是否促銷 |
props_name | String | 0 | 1627207:1347647754:顏色分類:長方形帶開瓶器+送工具刀卡+鏈子;1627207:1347647753:顏色分類:橢圓形帶開瓶器+送工具刀卡+鏈子; | 商品屬性名。格式為pid1:vid1:name1:value1;pid1:vid2:name2:value2。 |
prop_imgs | prop_imgs[] | 0 | 商品屬性圖片列表 | |
prop_img | prop_img[] | 0 | ||
properties | string | 0 | 1627207:1347647754 | |
url | string | 0 | //gd2.alicdn.com/imgextra/i3/2596264565/TB2.XeblVXXXXXkXpXXXXXXXXXX_!!2596264565.jpg | |
property_alias | String | 0 | 20509:9974422:36;1627207:28326:紅色;20509:9975710:38;1627207:28326:紅色;20509:9981357:40;1627207:28326:紅色 | 銷售屬性值別名。格式為pid1:vid1:alias1;pid1:vid2:alia2。 |
props | Mix | 0 | [{ "name": "產地","value": "中國" }] | 商品屬性 |
total_sold | Int | 0 | ||
skus | skus[] | 0 | 商品規格信息列表 | |
sku | sku[] | 0 | ||
price | string | 0 | 39 | |
total_price | string | 0 | ul | |
orginal_price | string | 0 | 39.00 | |
properties | string | 0 | 1627207:1347647754", | |
properties_name | string | 0 | 1627207:1347647754:顏色分類:長方形帶開瓶器+送工具刀卡+鏈子 | |
quantity | string | 0 | 305 | |
sku_id | string | 0 | 316659862598 | |
seller_id | Int | 0 | 2844096782 | 賣家ID |
sales | Int | 0 | 138 | 銷量 |
shop_id | Int | 0 | 151372205 | 店鋪ID |
props_list | Mix | 0 | {20509:9974422: 尺碼:36} | 商品屬性 |
seller_info | seller_info[] | 1 | 賣家信息 | |
nick | string | 1 | 歡樂購客棧 | |
city | string | 0 | ||
level | int | 0 | 12 | |
bail | int | 0 | 2000 | |
rate | int | 0 | 96 | |
score | float | 0 | 4.8 | |
delivery_score | float | 0 | 4.8 | |
item_score | float | 0 | 4.8 | |
shop_type | string | 0 | ||
user_num_id | Bigint | 0 | 2596264565 | |
sid | Bigint | 0 | 127203758 | |
title | string | 0 | 歡樂購客棧 | |
zhuy | string | 0 | //shop127203758.taobao.com | |
company_name | string | 0 | ||
menu | mix | 0 | [] | |
tmall | Boolean | 0 | false | 是否天貓 |
error | String | 0 | 錯誤信息 | |
warning | String | 0 | 警告信息 | |
url_log | Mix | 0 | [] | |
favcount | Int | 0 | 0 | |
fanscount | Int | 0 | 0 | |
method | String | 0 | item_tmall:pget_item | |
promo_type | String | 0 | ||
props_img | Mix | 0 | 1627207:28326": "//img.alicdn.com/imgextra/i2/2844096782/O1CN01VrjpXt1zyCc9DvERE_!!2844096782.jpg | 屬性圖片 |
shop_item | Mix | 0 | [] | |
relate_items | Mix | 0 | [] |