本文介绍如何通过阿里云云手机 Java SDK调用ListInstances查询一台或多台云手机实例。
前提条件
您必须至少创建了一台云手机实例。
背景信息
查询云手机实例适用于在众多实例中快速筛选出需要操作的实例的所需信息,例如:
在根据实例ID修改公网带宽前,根据实例计费方式、运行状态、公网带宽计费方式等筛选出符合条件的云手机实例。
在更新实例上部署的应用前,查询所有使用了相同镜像的云手机实例。
重要
此接口只支持
NextToken
查询方式。返回结果按照云手机创建时间顺序正序返回。
通过
NextToken
设置查询凭证(Token),其取值是上一次调用ListInstances
返回的NextToken
参数值,再通过MaxResults
设置单页查询的最大条目数。实例所在地域不同,
regionId
和domain
也不同,例:杭州地域regionId
为cn-hangzhou ,domain
为cloudphone.cn-hangzhou.aliyuncs.com。此示例通过阿里云Credentials工具从环境变量中读取AccessKey,来实现API访问的身份验证。具体配置方法,请参见身份验证配置。
代码示例
import com.alibaba.fastjson.JSONObject;
import com.aliyun.cloudphone20201230.Client;
import com.aliyun.cloudphone20201230.models.ListInstancesRequest;
import com.aliyun.cloudphone20201230.models.ListInstancesResponse;
import com.aliyun.teaopenapi.models.Config;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
public class ListInstances {
private static final String REGION_ID = "cn-shanghai";
private static final String DOMAIN = "cloudphone.cn-shanghai.aliyuncs.com";
public static void main(String[] args) throws Exception {
Config config = new Config();
config.setRegionId(REGION_ID);
config.setEndpoint(DOMAIN);
//阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
//强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
//通过阿里云Credentials工具从环境变量中读取AccessKey,来实现API访问的身份验证。
com.aliyun.credentials.Client credentialClient = new com.aliyun.credentials.Client();
config.setCredential(credentialClient);
Client client = new Client(config);
ListInstancesRequest request = new ListInstancesRequest();
request.setRegionId(REGION_ID);
ListInstancesResponse response;
try {
response = client.listInstances(request);
System.out.println(JSONObject.toJSONString(response.getBody()));
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
System.out.println("ErrCode:" + e.getErrCode());
System.out.println("ErrMsg:" + e.getErrMsg());
System.out.println("RequestId:" + e.getRequestId());
}
}
}
执行结果
实际返回结果为:
{
"Instances": {
"Instance": [
{
"Status": "Running",
"KeyPairName": "keypair-***",
"Description": "",
"ZoneId": "cn-hangzhou-i",
"InstanceId": "cp-bp******",
"SecurityGroupId": "sg-bp******",
"OsName": "Android 9.0",
"VpcAttributes": {
"PrivateIpAddress": "192.168.***.***",
"VSwitchId": "vsw-bp******"
},
"InstanceName": "云手机实例1",
"OsNameEn": "Android 9.0",
"ChargeType": "PostPaid",
"CreationTime": "2023-05-06T03:32Z",
"ImageId": "android_9_0_0_release_3854899_20221216.raw",
"EipAddress": {
"AllocationId": "",
"IpAddress": "",
"InternetChargeType": ""
},
"RegionId": "cn-hangzhou",
"InstanceType": "ecp.ce.xlarge",
"Resolution": "720*1280"
},
{
"Status": "Running",
"KeyPairName": "",
"Description": "",
"ZoneId": "cn-hangzhou-i",
"InstanceId": "cp-bp******",
"SecurityGroupId": "sg-bp******",
"OsName": "Android 9.0",
"VpcAttributes": {
"PrivateIpAddress": "172.16.***.***",
"VSwitchId": "vsw-bp******"
},
"InstanceName": "云手机实例2",
"OsNameEn": "Android 9.0",
"ChargeType": "PostPaid",
"CreationTime": "2023-03-23T07:51Z",
"ImageId": "android_9_0_0_release_3854899_20221216.raw",
"EipAddress": {
"Bandwidth": 5,
"AllocationId": "eip-bp******",
"IpAddress": "120.55.***.***",
"InternetChargeType": "PayByTraffic"
},
"RegionId": "cn-hangzhou",
"InstanceType": "ecp.ce.large",
"Resolution": "536*960"
}
]
},
"TotalCount": 2,
"NextToken": "AAAAAdiuCanYOryCfQ2ZP6",
"RequestId": "1E089A61-2267-58FB-B071-D23878******",
"MaxResults": 100
}
相关文档
文档内容是否对您有帮助?