當當網是中國知名的綜合性電子商務平臺之一,成立于 1999 年,總部位于北京。當當網的產品類別非常豐富,涵蓋了圖書、電子書、音像、影視、數碼、手機、家電、家居、母嬰、美妝等數十個品類??梢栽诋敭斁W上進行商品搜索,瀏覽不同分類的商品列表,并在其中選擇你感興趣的商品。當當網上有各種各樣的商品,包括圖書、電子產品、家居用品等。
Dangdang.item_search - 獲取當當網商品列表數據返回值說明
1. 公共參數:
名稱 | 類型 | 必須 | 描述 |
---|---|---|---|
key | String | 是 | 調用key(必須以GET方式拼接在URL中) |
secret | String | 是 | 調用密鑰(復制v:Taobaoapi2014) |
api_name | String | 是 | API接口名稱(包括在請求地址中)[item_search,item_get,item_search_shop等] |
cache | String | 否 | [yes,no]默認yes,將調用緩存的數據,速度比較快 |
result_type | String | 否 | [json,jsonu,xml,serialize,var_export]返回數據格式,默認為json,jsonu輸出的內容中文可以直接閱讀 |
lang | String | 否 | [cn,en,ru]翻譯語言,默認cn簡體中文 |
version | String | 否 | API版本 (查看接口調用演示) |
2.請求方式:HTTP POST GET
3.請求參數:
請求參數:q=鞋子&start_price=&end_price=&page=&cat=&discount_only=&sort=&page_size=&seller_info=&nick=&ppath=
參數說明:q:搜索關鍵字
cat:分類ID
start_price:開始價格
end_price:結束價格
sort:排序[bid,_bid,bid2,_bid2,_sale,_credit]
(bid:總價,bid2:商品價格,sale:銷量,credit信用,加_前綴為從大到小排序)
page:
4.Java請求示例:
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://api-vxx.Taobaoapi2014.cn/dangdang/item_search/?key=<您自己的apiKey>&secret=<您自己的apiSecret>&q=鞋子&start_price=&end_price=&page=&cat=&discount_only=&sort=&page_size=&seller_info=&nick=&ppath=";
JSONObject json = getRequestFromUrl(url);
System.out.println(json.toString());
}
}