AndroluaDocs AndroluaDocs
首页
文章
项目
资源
GitLab (opens new window)
首页
文章
项目
资源
GitLab (opens new window)
  • Lua

    • Lua初见
    • Lua语法学习
    • 数组
    • 函数&回调
  • 开发者素质基础
  • 安卓开发基础

    • 认识安卓开发
    • 认识activity
  • 制作第一个程序
  • 界面设计

    • 布局认知和Aly介绍
    • 布局表及控件认知
    • 自定义控件
    • 列表于相关实现
    • 布局动画相关
  • 数据处理

    • 各种格式的文件及解析方法
    • 正则表达式及lua字符串操作
    • 各种算法实现及数据安全
    • 软件数据存取策略
    • 文件压缩移动等
  • 网络相关

    • 常用网络库的使用
    • Api对接实践
      • 综述
        • 什么是Api
        • 分类?
        • Okhttp
    • Okhttp&Jsoup库使用
  • 系统操作

    • 应用软件间通信
    • 应用与硬件的交互
  • 源码安全

    • 框架原生保护与原理
    • 常用加密原理与实践
  • 文章
  • 网络相关
可可
2022-04-15
目录

Api对接实践

# 综述

Api的对接技术主要分 Api操作 , 编写Api工具类 ,处理Api返回数据

# 什么是Api

API即Application Programming Interface,也就是我们平时常说的‘接口’啦!接口就是其他应用构建的与外界交互的一套结构,这使开发人员更容易地创建炫酷的功能!

而在本文中所说的 Api 仅仅指 服务器为安卓应用的Api哦!

# 分类?

Api操作主要分 Get , Post 两大类。

当我们想从 服务API 访问数据时,我们就需要发出请求(POST)。比如,当你访问 bilibili.com 时,你的浏览器就会向服务器发出很多请求!

当当当!让我们隆重介绍在 Alua类工具中,基本自带Get请求的写法!

我们以 Tenapi-网易云歌曲信息获取 (opens new window)文档为例,详细解释常用的两种基本类方法

阅读文档,我们可以发现我们的 目标Api 为 https://tenapi.cn/wyyinfo/?id=1952597628

cjson=require("cjson")--Androlua+,引用了cjson
cookie=""
--cookie 是缓存!一个特殊的cookie可以使你维持登陆状态。
header={
  ["User-Agent"]= "Mozilla/5.0 (Linux; U; Android 2.3.7; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
}
--header 是请求头,告诉服务器你的浏览器信息,系统类型~
Http.get("https://tenapi.cn/wyyinfo/?id=1952597628",cookie,"utf8",header,function(code,content,cookie,header)--访问完毕传出的四个参数:状态码,内容,cookie,header
      if(code==200 and content)then
      --确认数据返回!
      --这里其实是异步方法的回调函数里面哦!
        json=cjson.decode(content);
        if json["code"]==200 then --200是数字类型
          json_data = json["data"];
          Name = json_data["songs"];--使用data字段里面的属性
          Author = json_data["sings"];
          MusicUrl = tostring(json_data["url"]);
          print("通信成功"..code..Name..MusicUrl)--注意数字int类型
         else
          print("访问正常,但是Api返回失败信息!"..json["code"])
        end
       else
        print("通信失败"..code)
      end
      --因为是异步所以不return
    end)

结果(经过在线格式化 (opens new window) )

{
	"code": 200,
	"data": {
		"id": 1952597628,
		"songs": "晚笑话",
		"sings": "向晚Ava",
		"album": "晚笑话",
		"cover": "http://p2.music.126.net/cncwdwVyshBLQSS0grD1ig==/109951167501515675.jpg",
		"url": "https://music.163.com/song/media/outer/url?id=1952597628.mp3"
	}
}

那么Post方法又是怎么一回事呢?

它经常在 提交数据/查询/登陆(获取cookie等等) 等功能出现!因此,它比Get参数要多一个哦!

我们这里使用网易云的音乐搜索Api (opens new window): https://music.163.com/api/search/pc?s=晚笑话&offset=0&limit=1&type=1

POST http://music.163.com/api/search/pc
BODY s:PAPISM offset:0 limit:1 type:1

其实大家可以看出来,有时候,网站?参数1=001&参数2=002也是可以拿到一些接口(例如不需要提交文件)的数据的....

require "import"
import"http" --有的AIDE不需要这个,不能运行则删
cjson=require("cjson")--Androlua+,引用了cjson
header={
    ["User-Agent"]= "Mozilla/5.0 (Windows NT 7.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904/developer",
    ["Content-Type"]="application/json" --这里具体看文档哦
}
data={
  [s]="晚笑话",
  [offset]=0,
  [limit]=1,
  [type]=1
}
--或者使用 data=("s=晚笑话&offset=0&limit=1&type=1")

Http.post("https://music.163.com/api/search/pc",data,cookie,"utf8",header,function(code,content,cookie,header)--这里cookie传入函数!
    if code==200 then
      --模糊确认(content:find("ok"))
        print("服务器的数据很正常! ✧٩(ˊωˋ*)و✧")
        json=cjson.decode(content);
        if json["code"]==200 then --200是数字类型
           name = json["result"]["songs"]["name"];
           print("serach result:"..tostring(name))--注意!对于可能为列表的键,print时请务必tostring或者做判断!!!
        else
           print("服务器提供的数据不对劲! Σ( ° △ °|||)︴"..content) --可能会遇到网易云屏蔽情况,没关系!
        end
    else
        print('Failed post'..code)
    end
end)

而对于上传文件来说,接口的file字段可以后跟文件地址。

fileMap
{[key]=[Path]}

Api可能的响应字段如下

{
	"result": {
		"songs": [{
			"name": "晚笑话",
			"id": 1952597628,
			"position": 0,
			"alias": [],
			"status": 0,
			"fee": 0,
			"copyrightId": 0,
			"disc": "01",
			"no": 1,
			"artists": [{
				"name": "向晚Ava",
				"id": 0,
				"picId": 0,
				"img1v1Id": 0,
				"briefDesc": "",
				"picUrl": "https://p2.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
				"img1v1Url": "https://p2.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
				"albumSize": 0,
				"alias": [],
				"trans": "",
				"musicSize": 0
			}, {
				"name": "隳",
				"id": 49193991,
				"picId": 0,
				"img1v1Id": 0,
				"briefDesc": "",
				"picUrl": "https://p2.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
				"img1v1Url": "https://p2.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
				"albumSize": 0,
				"alias": [],
				"trans": "",
				"musicSize": 0
			}],
			"album": {
				"name": "晚笑话",
				"id": 145870297,
				"type": "专辑",
				"size": 1,
				"picId": 109951167501515675,
				"blurPicUrl": "https://p2.music.126.net/cncwdwVyshBLQSS0grD1ig==/109951167501515675.jpg",
				"companyId": 0,
				"pic": 109951167501515675,
				"picUrl": "https://p2.music.126.net/cncwdwVyshBLQSS0grD1ig==/109951167501515675.jpg",
				"publishTime": 1654089992516,
				"description": "",
				"tags": "",
				"company": null,
				"briefDesc": "",
				"artist": {
					"name": "",
					"id": 0,
					"picId": 0,
					"img1v1Id": 0,
					"briefDesc": "",
					"picUrl": "https://p2.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
					"img1v1Url": "https://p2.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
					"albumSize": 0,
					"alias": [],
					"trans": "",
					"musicSize": 0
				},
				"songs": [],
				"alias": [],
				"status": 0,
				"copyrightId": 0,
				"commentThreadId": "R_AL_3_145870297",
				"artists": [{
					"name": "隳",
					"id": 49193991,
					"picId": 0,
					"img1v1Id": 0,
					"briefDesc": "",
					"picUrl": "https://p2.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
					"img1v1Url": "https://p2.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
					"albumSize": 0,
					"alias": [],
					"trans": "",
					"musicSize": 0
				}],
				"picId_str": "109951167501515675"
			},
			"starred": false,
			"popularity": 85.0,
			"score": 85,
			"starredNum": 0,
			"duration": 136641,
			"playedNum": 0,
			"dayPlays": 0,
			"hearTime": 0,
			"ringtone": "",
			"crbt": null,
			"audition": null,
			"copyFrom": "",
			"commentThreadId": "R_SO_4_1952597628",
			"rtUrl": null,
			"ftype": 0,
			"rtUrls": [],
			"copyright": 0,
			"rtype": 0,
			"rurl": null,
			"hMusic": {
				"name": null,
				"id": 7333206889,
				"size": 5468205,
				"extension": "mp3",
				"sr": 48000,
				"dfsId": 0,
				"bitrate": 320000,
				"playTime": 136641,
				"volumeDelta": 45083.0
			},
			"mMusic": {
				"name": null,
				"id": 7333206886,
				"size": 3280941,
				"extension": "mp3",
				"sr": 48000,
				"dfsId": 0,
				"bitrate": 192000,
				"playTime": 136641,
				"volumeDelta": 47723.0
			},
			"lMusic": {
				"name": null,
				"id": 7333206883,
				"size": 2187309,
				"extension": "mp3",
				"sr": 48000,
				"dfsId": 0,
				"bitrate": 128000,
				"playTime": 136641,
				"volumeDelta": 49493.0
			},
			"bMusic": {
				"name": null,
				"id": 7333206883,
				"size": 2187309,
				"extension": "mp3",
				"sr": 48000,
				"dfsId": 0,
				"bitrate": 128000,
				"playTime": 136641,
				"volumeDelta": 49493.0
			},
			"mvid": 0,
			"mp3Url": "http://m2.music.126.net/hmZoNQaqzZALvVp0rE7faA==/0.mp3"
		}],
		"songCount": 5
	},
	"code": 200
}

# Okhttp

OKhttp参见下一节!

Gitlab在线编辑&贡献你的智慧 (opens new window)
上次更新: 2022/12/27, 01:33:36
常用网络库的使用
Okhttp&Jsoup库使用

← 常用网络库的使用 Okhttp&Jsoup库使用→

Theme by Vdoing | Copyright © 2022-2022 Created by coco | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式