1、API调用的第一步:购买API接口
关于API的购买这里就不过多陈述,网上一搜一大把,我这次教程使用的API是天行数据的API
2、获取ID和Key (类似账号密码)
这里就把我的KEY送给大家免费调用:
这个是查询全国疫情信息的API—可使用次数:100000
通过我写对应api的例子我发现,其实就两种情况:一种是get方式提交数据,另外一种是post方式提交数据 1.get方式提交数据,我们用获取云盘的信息为例: 获取云盘信息前我们要知道,我们要准备好什么数据:请求参数: url: 标明我们要访问的网址。
key=d557d60eeebe9fa695424a6e3930a8fd
Alias用于指定API函数的别名,如果我们调用的API函数要使用字符串(参数中包含String型)的话,Alias关键字是必须的。这是因为在ANSI和Unicode字符集中同一API函数的名称可能是不一样的,为了保证不出现声明错误,我们使用Alias关。
3.使用你需要的请求示例(API文档里会有)
3.使用API文档里的请求示例()
public class DataTest {
public static String request(String httpUrl,String httpArg) {
BufferedReader reader = null;
String result = null;
StringBuffer sbf = new StringBuffer();
/httpUrl = httpUrl + &34; + httpArg;
try {
URL url = new URL(httpUrl);
api接口怎么调用,HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod(&34;);
InputStream is = connection.getInputStream();
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sbf.append(strRead);
sbf.append(&34;);
}
reader.close();
result = sbf.toString();
} catch (Exception e) {
e.printStackTrace();
具体步骤如下: 首先,先登录Face++的官网注册账号 注册之后会获取到api_secret和api_key,这些在调用接口的时候需要用到。 然后接下来的就是使用PHP脚本调用API了。 在使用PHP开发微信公共平台的时候,推荐使用Github上的一款。
}
return result;
}
public static void main(String[] args) {
**
* @param urlAll
* :请求接口
* @param httpArg
比如我要使用sendmessage这个API函数,可以这样写:Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Lo。
* :参数
* @return 返回结果
*
String httpUrl = ";;
String httpArg=&34;;
System.out.println(request(httpUrl,httpArg));
}
}
测试效果:
4.请求参数
5、添加请求参数,查询2020年3月12日的全国疫情数据
修改后的代码
BufferedReader reader = null;
String result = null;
StringBuffer sbf = new StringBuffer();
httpUrl = httpUrl + &34; + httpArg;
try {
URL url = new URL(httpUrl);
.openConnection();
connection.setRequestMethod(&34;);
InputStream is = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(is,&34;));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sbf.append(strRead);
sbf.append(&34;);
1.注册用户 2.创建应用 3.申请接口调用权限 4.下载SDK 5.使用sdk编写代码 6.运行,取得返回的字符串结果 7.使用自己喜欢的方式将字符串反序列化得到JSON对象
}
reader.close();
result = sbf.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
**
* @param urlAll
* :请求接口
* @param httpArg
* :参数
* @return 返回结果
*
String httpUrl = ";;
String httpArg=&34;;
System.out.println(request(httpUrl,httpArg));
}
}
测试结果