api接口的获取调用方式是什么?

API接口的获取调用方式,通常分为以下几个步骤:

api接口的获取调用方式是什么?_第1张图片

1.注册账号并申请API Key:在API服务提供商的官方网站上注册账号,并申请API Key(包括通行证ID和密钥),以便后面的API调用验证。

api接口的获取调用方式是什么?_第2张图片 2.查看API接口文档:根据API服务提供商的官方文档,查看API接口的说明、参数、返回数据格式等信息。

3.构造API请求:根据API接口文档提供的参数格式和请求方式,构造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://taobao/item_get/?key=<您自己的apiKey>&secret=<您自己的apiSecret>&num_iid=652874751412&is_promotion=1";
		JSONObject json = getRequestFromUrl(url);
		System.out.println(json.toString());
	}

}

4.发送API请求并接收响应:使用相关的编程语言和库(如Python中的requests库、Java中的HttpURLConnection类等),发送API请求,并接收API响应结果。

{
	"item": {
		"num_iid": "652874751412",
		"title": "北欧轻奢布艺沙发 小户型简约现代客厅ins风网红款三双人订制沙发",
		"desc_short": "",
		"price": "480.00",
		"total_price": 0,
		"suggestive_price": 0,
		"orginal_price": "480.00",
		"nick": "惜情yqq1127",
		"num": "200",
		"min_num": 0,
		"detail_url": "https://item.taobao.com/item.htm?id=652874751412",
		"pic_url": "//img.alicdn.com/imgextra/i4/2568161054/O1CN01aYBriY1Jem9UDtt9e_!!2568161054.jpg",
		"brand": "#0 工厂",
		"brandId": "1435187098",
		"rootCatId": "50008164",
		"cid": "50020632",
		"crumbs": [],
		"created_time": "",
		"modified_time": "",
		"delist_time": "",
		"desc": "",
		"desc_img": [],
		"item_imgs": [
			{
				"url": "//img.alicdn.com/imgextra/i4/2568161054/O1CN01aYBriY1Jem9UDtt9e_!!2568161054.jpg"
			},
			{
				"url": "//img.alicdn.com/imgextra/i3/2568161054/O1CN01kjOfNb1Jem9DmWn8Y_!!2568161054.jpg"
			},
			{
				"url": "//img.alicdn.com/imgextra/i1/2568161054/O1CN01HoB9ha1Jem9DmWn8r_!!2568161054.jpg"
			},
			{
				"url": "//img.alicdn.com/imgextra/i4/2568161054/O1CN011PjP2P1Jem9MXEUFT_!!2568161054.jpg"
			},
			{
				"url": "//img.alicdn.com/imgextra/i3/2568161054/O1CN01KUfBFL1Jem9KTTMn1_!!2568161054.jpg"
			}
		],
		"item_weight": "",
		"item_size": "",
		"location": "江苏南通",
		"post_fee": "",
		"express_fee": null,
		"ems_fee": "",
		"shipping_to": "",
		"has_discount": "false",
		"video": {
			"url": null
		},
		"is_virtual": "",
		"sample_id": "",
		"is_promotion": "false",
		"props_name": "31480:14306495906:几人坐:脚踏90*60*48cm;31480:14306495907:几人坐:双人165*95*67cm;31480:14306495908:几人坐:三人210*95*67cm;31480:14306495909:几人坐:单人100*95*67cm;31480:21480914361:几人坐:四人位240*95*67cm;31480:21480914362:几人坐:大四人320*95*76cm;31480:1387571900:几人坐:3米贵妃沙发;31480:32527954:几人坐:定制尺寸;1627207:28321:颜色分类:乳白色 尺寸颜色可定制",
		"prop_imgs": {
			"prop_img": [
				{
					"properties": "1627207:28321",
					"url": "//img.alicdn.com/imgextra/i1/2568161054/O1CN017GTZ4h1Jem9Qra1ap_!!2568161054.jpg"
				}
			]
		},
		"property_alias": "",
		"props": [
			{
				"name": "品牌",
				"value": "#0 工厂"
			},
			{
				"name": "型号",
				"value": "520"
			},
			{
				"name": "材质",
				"value": "木"
			},
			{
				"name": "木质材质",
				"value": "松木"
			},
			{
				"name": "面料",
				"value": "绒布"
			},
			{
				"name": "风格",
				"value": "北欧"
			},
			{
				"name": "几人坐",
				"value": "脚踏90*60*48cm,双人165*95*67cm,三人210*95*67cm,单人100*95*67cm,四人位240*95*67cm,大四人320*95*76cm,3米贵妃沙发,定制尺寸"
			},
			{
				"name": "颜色分类",
				"value": "乳白色"
			},
			{
				"name": "填充物",
				"value": "海绵"
			},
			{
				"name": "结构工艺",
				"value": "木质工艺"
			},
			{
				"name": "是否可定制",
				"value": "是"
			},
			{
				"name": "沙发组合形式",
				"value": "U形"
			},
			{
				"name": "是否可拆洗",
				"value": "是"
			},
			{
				"name": "适用对象",
				"value": "成年人"
			},
			{
				"name": "是否带储物空间",
				"value": "否"
			},
			{
				"name": "产地",
				"value": "上海"
			},
			{
				"name": "地市",
				"value": "上海市"
			},
			{
				"name": "区县",
				"value": "奉贤区"
			},
			{
				"name": "是否组装",
				"value": "否"
			},
			{
				"name": "出租车是否可运输",
				"value": "否"
			},
			{
				"name": "填充物硬度",
				"value": "软"
			},
			{
				"name": "款式定位",
				"value": "经济型"
			}
		],
		"total_sold": "604",
		"skus": {
			"sku": [
				{
					"price": "480",
					"total_price": 0,
					"orginal_price": "480",
					"properties": "31480:14306495906;1627207:28321",
					"properties_name": "31480:14306495906:几人坐:脚踏90*60*48cm;1627207:28321:颜色分类:乳白色 尺寸颜色可定制",
					"quantity": "200",
					"sku_id": "4881047531343"
				},
				{
					"price": "1688",
					"total_price": 0,
					"orginal_price": "1688",
					"properties": "31480:14306495907;1627207:28321",
					"properties_name": "31480:14306495907:几人坐:双人165*95*67cm;1627207:28321:颜色分类:乳白色 尺寸颜色可定制",
					"quantity": "141",
					"sku_id": "4881047531344"
				},
				{
					"price": "2088",
					"total_price": 0,
					"orginal_price": "2088",
					"properties": "31480:14306495908;1627207:28321",
					"properties_name": "31480:14306495908:几人坐:三人210*95*67cm;1627207:28321:颜色分类:乳白色 尺寸颜色可定制",
					"quantity": "200",
					"sku_id": "4881047531345"
				},
				{
					"price": "968",
					"total_price": 0,
					"orginal_price": "968",
					"properties": "31480:14306495909;1627207:28321",
					"properties_name": "31480:14306495909:几人坐:单人100*95*67cm;1627207:28321:颜色分类:乳白色 尺寸颜色可定制",
					"quantity": "200",
					"sku_id": "4881047531346"
				},
				{
					"price": "2388",
					"total_price": 0,
					"orginal_price": "2388",
					"properties": "31480:21480914361;1627207:28321",
					"properties_name": "31480:21480914361:几人坐:四人位240*95*67cm;1627207:28321:颜色分类:乳白色 尺寸颜色可定制",
					"quantity": "200",
					"sku_id": "5039985183001"
				},
				{
					"price": "3188",
					"total_price": 0,
					"orginal_price": "3188",
					"properties": "31480:21480914362;1627207:28321",
					"properties_name": "31480:21480914362:几人坐:大四人320*95*76cm;1627207:28321:颜色分类:乳白色 尺寸颜色可定制",
					"quantity": "200",
					"sku_id": "5039985183002"
				},
				{
					"price": "3400",
					"total_price": 0,
					"orginal_price": "3400",
					"properties": "31480:1387571900;1627207:28321",
					"properties_name": "31480:1387571900:几人坐:3米贵妃沙发;1627207:28321:颜色分类:乳白色 尺寸颜色可定制",
					"quantity": "200",
					"sku_id": "5039984824000"
				},
				{
					"price": "3000",
					"total_price": 0,
					"orginal_price": "3000",
					"properties": "31480:32527954;1627207:28321",
					"properties_name": "31480:32527954:几人坐:定制尺寸;1627207:28321:颜色分类:乳白色 尺寸颜色可定制",
					"quantity": "200",
					"sku_id": "5039985183003"
				}
			]
		},
		"seller_id": "2568161054",
		"sales": 53,
		"shop_id": "567158267",
		"props_list": {
			"31480:14306495906": "几人坐:脚踏90*60*48cm",
			"31480:14306495907": "几人坐:双人165*95*67cm",
			"31480:14306495908": "几人坐:三人210*95*67cm",
			"31480:14306495909": "几人坐:单人100*95*67cm",
			"31480:21480914361": "几人坐:四人位240*95*67cm",
			"31480:21480914362": "几人坐:大四人320*95*76cm",
			"31480:1387571900": "几人坐:3米贵妃沙发",
			"31480:32527954": "几人坐:定制尺寸",
			"1627207:28321": "颜色分类:乳白色 尺寸颜色可定制"
		},
		"seller_info": {
			"nick": "惜情yqq1127",
			"item_score": "4.9 ",
			"score_p": "4.9 ",
			"delivery_score": "4.9 ",
			"shop_type": "C",
			"user_num_id": "2568161054",
			"sid": "567158267",
			"title": "现代布艺沙发",
			"zhuy": "https://shop567158267.taobao.com/",
			"shop_name": "现代布艺沙发"
		},
		"tmall": false,
		"error": "",
		"warning": "",
		"url_log": [],
		"fav_count": "7818",
		"fans_count": "663",
		"freight": "平邮: 快递包邮",
		"props_imgs": {
			"prop_img": [
				{
					"properties": "1627207:28321",
					"url": "//img.alicdn.com/imgextra/i1/2568161054/O1CN017GTZ4h1Jem9Qra1ap_!!2568161054.jpg"
				}
			]
		},
		"_ddf": "xdl",
		"promo_type": null,
		"props_img": {
			"1627207:28321": "//img.alicdn.com/imgextra/i1/2568161054/O1CN017GTZ4h1Jem9Qra1ap_!!2568161054.jpg"
		},
		"format_check": "ok",
		"shop_item": [],
		"relate_items": []
	},
	"error": "",
	"secache": "4ad7ad2480af253fec9c2fd4daa266bb",
	"secache_time": 1687330167,
	"secache_date": "2023-06-21 14:49:27",
	"translate_status": "",
	"translate_time": 0,
	"language": {
		"default_lang": "cn",
		"current_lang": "cn"
	},
	"reason": "",
	"error_code": "0000",
	"cache": 0,
	"api_info": "today:130 max:10100 all[221=130+6+85];expires:2030-12-31",
	"execution_time": "1.646",
	"server_time": "Beijing/2023-06-21 14:49:27",
	"client_ip": "115.153.49.96",
	"call_args": {
		"num_iid": "652874751412",
		"is_promotion": "1"
	},
	"api_type": "taobao",
	"translate_language": "zh-CN",
	"translate_engine": "baidu",
	"server_memory": "6.14MB",
	"request_id": "gw-1.64929d75a8ea6",
	"last_id": "1822571582"
}

5.解析API响应数据:根据API接口文档返回数据格式,对API响应进行解析,并将所需结果提取出来。

6.处理API响应结果:根据API响应结果进行相应的数据处理和操作,如数据展示、数据存储、数据分析等。

需要注意的是,不同的API服务提供商可能会有不同的API调用认证方式、请求方式、参数格式等。因此,在使用API接口之前,需要仔细阅读API文档并按照规范进行调用。

你可能感兴趣的:(api,学习分享,数据库,java,面试,.net)