You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
324 lines
8.9 KiB
324 lines
8.9 KiB
6 years ago
|
# 开发SDK
|
||
|
|
||
|
开放平台把接口开发完毕后,一般需要开发对应的SDK,提供给ISV。SOP提供了一个基础的SDK开发包
|
||
|
|
||
|
开发者可以在此基础上做开发,就拿sdk-java来说,具体步骤如下:
|
||
|
|
||
6 years ago
|
## sdk-java
|
||
6 years ago
|
|
||
|
开放平台对应的sdk,适用于Android。SDK依赖了三个jar包
|
||
|
|
||
|
- okhttp.jar 用于网络请求
|
||
|
- fastjson-android.jar 用于json处理
|
||
|
- commons-logging.jar 日志处理
|
||
|
|
||
6 years ago
|
### 接口封装步骤
|
||
6 years ago
|
|
||
|
比如获取故事信息接口
|
||
|
|
||
|
- 接口名:alipay.story.find
|
||
|
- 版本号:1.0
|
||
|
- 参数:name 故事名称
|
||
|
- 返回信息
|
||
|
|
||
|
```
|
||
|
{
|
||
|
"alipay_story_find_response": {
|
||
|
"msg": "Success",
|
||
|
"code": "10000",
|
||
|
"name": "白雪公主",
|
||
|
"id": 1,
|
||
|
"gmtCreate": 1554193987378
|
||
|
},
|
||
|
"sign": "xxxxx"
|
||
|
}
|
||
|
```
|
||
|
|
||
|
针对这个接口,封装步骤如下:
|
||
|
|
||
|
1.在`model`包下新建一个类,定义业务参数
|
||
|
|
||
|
|
||
|
```java
|
||
|
@Data
|
||
|
public class GetStoryModel {
|
||
|
|
||
|
@JSONField(name = "name")
|
||
|
private String name;
|
||
|
}
|
||
|
```
|
||
|
|
||
|
2.在`response`包下新建一个返回类GetStoryResponse,继承`BaseResponse`
|
||
|
|
||
|
里面填写返回的字段
|
||
|
|
||
|
```
|
||
|
@Data
|
||
|
public class GetStoryResponse extends BaseResponse {
|
||
|
private Long id;
|
||
|
private String name;
|
||
|
private Date gmtCreate;
|
||
|
}
|
||
|
```
|
||
|
|
||
|
3.在`request`包下新建一个请求类,继承`BaseRequest`
|
||
|
|
||
|
BaseRequest中有个泛型参数,填`GetStoryResponse`类,表示这个请求对应的返回类。
|
||
|
重写`method()`方法,填接口名。
|
||
|
|
||
|
如果要指定版本号,可重写`version()`方法,或者后续使用`request.setVersion(version)`进行设置
|
||
|
|
||
|
```java
|
||
|
public class GetStoryRequest extends BaseRequest<GetStoryResponse> {
|
||
|
@Override
|
||
|
protected String method() {
|
||
|
return "alipay.story.find";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
```
|
||
|
|
||
6 years ago
|
### 使用方式
|
||
6 years ago
|
|
||
|
```java
|
||
|
String url = "http://localhost:8081/api"; // zuul
|
||
|
String appId = "2019032617262200001";
|
||
|
String privateKey = "你的私钥";
|
||
|
|
||
|
// 声明一个就行
|
||
|
OpenClient client = new OpenClient(url, appId, privateKey);
|
||
|
|
||
|
// 标准用法
|
||
|
@Test
|
||
|
public void testGet() {
|
||
|
// 创建请求对象
|
||
|
GetStoryRequest request = new GetStoryRequest();
|
||
|
// 请求参数
|
||
|
GetStoryModel model = new GetStoryModel();
|
||
|
model.setName("白雪公主");
|
||
|
|
||
|
request.setBizModel(model);
|
||
|
|
||
|
// 发送请求
|
||
|
GetStoryResponse response = client.execute(request);
|
||
|
|
||
|
if (response.isSuccess()) {
|
||
|
// 返回结果
|
||
|
System.out.println(response);
|
||
|
} else {
|
||
|
System.out.println(response);
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
6 years ago
|
### 使用方式2(懒人版)
|
||
6 years ago
|
|
||
|
如果不想添加Request,Response,Model。可以用这种方式,返回body部分是字符串,后续自己处理
|
||
|
|
||
|
body对应的是alipay_story_find_response部分
|
||
|
|
||
|
```java
|
||
|
@Test
|
||
|
public void testLazy() {
|
||
|
// 创建请求对象
|
||
|
CommonRequest request = new CommonRequest("alipay.story.find");
|
||
|
// 请求参数
|
||
|
Map<String, Object> bizModel = new HashMap<>();
|
||
|
bizModel.put("name", "白雪公主");
|
||
|
request.setBizModel(bizModel);
|
||
|
|
||
|
// 发送请求
|
||
|
CommonResponse response = client.execute(request);
|
||
|
|
||
|
if (response.isSuccess()) {
|
||
|
// 返回结果,body对应的是alipay_story_find_response部分
|
||
|
String body = response.getBody();
|
||
|
JSONObject jsonObject = JSON.parseObject(body);
|
||
|
System.out.println(jsonObject);
|
||
|
} else {
|
||
|
System.out.println(response);
|
||
|
}
|
||
|
}
|
||
|
```
|
||
6 years ago
|
|
||
|
## sdk-.net
|
||
|
|
||
|
|
||
|
### 接口封装步骤
|
||
|
|
||
|
比如获取故事信息接口
|
||
|
|
||
|
- 接口名:alipay.story.find
|
||
|
- 版本号:1.0
|
||
|
- 参数:name 故事名称
|
||
|
- 返回信息
|
||
|
|
||
|
```
|
||
|
{
|
||
|
"alipay_story_find_response": {
|
||
|
"msg": "Success",
|
||
|
"code": "10000",
|
||
|
"name": "白雪公主",
|
||
|
"id": 1,
|
||
|
"gmtCreate": 1554193987378
|
||
|
},
|
||
|
"sign": "xxxxx"
|
||
|
}
|
||
|
```
|
||
|
|
||
|
针对这个接口,封装步骤如下:
|
||
|
|
||
|
1.在`Model`包下新建一个类,定义业务参数
|
||
|
|
||
|
```
|
||
|
namespace SDKCSharp.Model
|
||
|
{
|
||
|
public class GetStoryModel
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// 故事名称
|
||
|
/// </summary>
|
||
|
/// <value>The name.</value>
|
||
|
[JsonProperty("name")]
|
||
|
public string Name { get; set; }
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
`[JsonProperty("name")]`是Newtonsoft.Json组件中的类,用于Json序列化,括号中的是参数名称。
|
||
|
类似于Java中的注解,`@JSONField(name = "xx")`
|
||
|
|
||
|
2.在`Response`包下新建一个返回类GetStoryResponse,继承`BaseResponse`
|
||
|
|
||
|
里面填写返回的字段
|
||
|
|
||
|
```
|
||
|
namespace SDKCSharp.Response
|
||
|
{
|
||
|
public class GetStoryResponse: BaseResponse
|
||
|
{
|
||
|
[JsonProperty("id")]
|
||
|
public int Id { get; set; }
|
||
|
|
||
|
[JsonProperty("name")]
|
||
|
public string Name { get; set; }
|
||
|
|
||
|
[JsonProperty("gmt_create")]
|
||
|
public string GmtCreate { get; set; }
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
```
|
||
|
|
||
|
3.在`Request`文件夹下新建一个请求类,继承`BaseRequest`
|
||
|
|
||
|
BaseRequest中有个泛型参数,填`GetStoryResponse`类,表示这个请求对应的返回类。
|
||
|
重写`GetMethod()`方法,填接口名。
|
||
|
|
||
|
如果要指定版本号,可重写`GetVersion()`方法,或者后续使用`request.Version = version`进行设置
|
||
|
|
||
|
```
|
||
|
namespace SDKCSharp.Request
|
||
|
{
|
||
|
public class GetStoryRequest : BaseRequest<GetStoryResponse>
|
||
|
{
|
||
|
public override string GetMethod()
|
||
|
{
|
||
|
return "alipay.story.find";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
```
|
||
|
|
||
|
|
||
|
|
||
|
### 使用方式
|
||
|
|
||
|
```
|
||
|
class MainClass
|
||
|
{
|
||
|
static string url = "http://localhost:8081/api"; // zuul
|
||
|
static string appId = "2019032617262200001";
|
||
|
// 支付宝私钥
|
||
|
static string privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCXJv1pQFqWNA/++OYEV7WYXwexZK/J8LY1OWlP9X0T6wHFOvxNKRvMkJ5544SbgsJpVcvRDPrcxmhPbi/sAhdO4x2PiPKIz9Yni2OtYCCeaiE056B+e1O2jXoLeXbfi9fPivJZkxH/tb4xfLkH3bA8ZAQnQsoXA0SguykMRZntF0TndUfvDrLqwhlR8r5iRdZLB6F8o8qXH6UPDfNEnf/K8wX5T4EB1b8x8QJ7Ua4GcIUqeUxGHdQpzNbJdaQvoi06lgccmL+PHzminkFYON7alj1CjDN833j7QMHdPtS9l7B67fOU/p2LAAkPMtoVBfxQt9aFj7B8rEhGCz02iJIBAgMBAAECggEARqOuIpY0v6WtJBfmR3lGIOOokLrhfJrGTLF8CiZMQha+SRJ7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vrKL6AAVNK7eQZCRDk9KQz+0GKOGqku0nOZjUbAu6A2/vtXAaAuFSFx1rUQVVjFulLexkXR3KcztL1Qu2k5pB6Si0K/uwQ=";
|
||
|
|
||
|
|
||
|
// 声明一个就行
|
||
|
static OpenClient client = new OpenClient(url, appId, privateKey);
|
||
|
|
||
|
public static void Main(string[] args)
|
||
|
{
|
||
|
TestGet();
|
||
|
}
|
||
|
|
||
|
// 标准用法
|
||
|
private static void TestGet()
|
||
|
{
|
||
|
// 创建请求对象
|
||
|
GetStoryRequest request = new GetStoryRequest();
|
||
|
// 请求参数
|
||
|
GetStoryModel model = new GetStoryModel();
|
||
|
model.Name = "白雪公主";
|
||
|
request.BizModel = model;
|
||
|
|
||
|
// 发送请求
|
||
|
GetStoryResponse response = client.Execute(request);
|
||
|
|
||
|
if (response.IsSuccess())
|
||
|
{
|
||
|
// 返回结果
|
||
|
Console.WriteLine("故事名称:{0}", response.Name);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
Console.WriteLine("错误, code:{0}, msg:{1}, subCode:{2}, subMsg:{3}",
|
||
|
response.Code, response.Msg, response.SubCode, response.SubMsg);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
```
|
||
|
|
||
|
### 使用方式2(懒人版)
|
||
|
|
||
|
如果不想添加Request,Response,Model。可以用这种方式,返回data部分是Dictionary<string, object>,后续自己处理
|
||
|
|
||
|
```
|
||
|
// 懒人版,如果不想添加Request,Response,Model。可以用这种方式,返回Dictionary<string, object>,后续自己处理
|
||
|
private static void TestCommon()
|
||
|
{
|
||
|
// 创建请求对象
|
||
|
CommonRequest request = new CommonRequest("alipay.story.find");
|
||
|
// 请求参数
|
||
|
Dictionary<string, string> bizModel = new Dictionary<string, string>
|
||
|
{
|
||
|
["name"] = "白雪公主"
|
||
|
};
|
||
|
|
||
|
request.BizModel = bizModel;
|
||
|
|
||
|
// 发送请求
|
||
|
CommonResponse response = client.Execute(request);
|
||
|
|
||
|
if (response.IsSuccess())
|
||
|
{
|
||
|
// 返回结果
|
||
|
string body = response.Body;
|
||
|
Dictionary<string, object> dict = JsonUtil.ParseToDictionary(body);
|
||
|
|
||
|
Console.WriteLine("Dictionary内容:");
|
||
|
foreach (var item in dict)
|
||
|
{
|
||
|
Console.WriteLine("{0}:{1}", item.Key, item.Value);
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
Console.WriteLine("错误, code:{0}, msg:{1}, subCode:{2}, subMsg:{3}",
|
||
|
response.Code, response.Msg, response.SubCode, response.SubMsg);
|
||
|
}
|
||
|
}
|
||
|
```
|