0
点赞
收藏
分享

微信扫一扫

Json根据商品ID获取天猫商品详情数据API


l 接口名称 :Taobao.item_get

l 功能说明:通过 api 接口,输入单个商品 id,得到所有相关商品的全部信息

l 应用级输入参数 :商品 ID,复制 Taobaoapi2014 拿到请求 key 和 secret

l 请求方式:HTTP POST GET ;演示地址.

l 请求示例 :

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.xxxx.cn/taobao/item_get/?key=<您自己的apiKey>&secret=<您自己的apiSecret>&num_iid=商品ID&is_promotion=1";
		JSONObject json = getRequestFromUrl(url);
		System.out.println(json.toString());
	}

}

以上示例中,通过发送 HTTP /POST/GET请求获取唯品会商品详情的 API 数据,然后将返回的 JSON 数据解析为对应的数据结构,以便进一步处理和使用。请注意,实际情况中,您还需要根据唯品会的 API 文档和要求来构建请求、处理异常情况和其他实现细节。

举报

相关推荐

0 条评论