云服务器内容精选

  • 请求签名与API调用 在Android工程中的“app/libs”目录下,加入SDK所需jar包。其中jar包必须包括: java-sdk-core-x.x.x.jar commons-logging-1.2.jar joda-time-2.9.9.jar 在“build.gradle”文件中加入okhttp库的依赖。 在“build.gradle”文件中的“dependencies”下加入“implementation 'com.squareup.okhttp3:okhttp:3.11.0'”。 1 2 3 4 5 dependencies { ... ... implementation 'com.squareup.okhttp3:okhttp:3.11.0' } 创建request,输入AK和SK,并指定域名、方法名、请求uri和body。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Request request = new Request(); try { // Directly writing AK/SK in code is risky. For security, encrypt your AK/SK and store them in the configuration file or environment variables. // In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, set environment variables HUAWEICLOUD_SDK_AK and HUAWEICLOUD_SDK_SK. request.setKey(System.getenv("HUAWEICLOUD_SDK_AK")); request.setSecret(System.getenv("HUAWEICLOUD_SDK_SK")); request.setMethod("GET"); request.setUrl("https://service.region.example.com3/v1/{project_id}/vpcs"); request.addQueryStringParam("name", "value"); request.addHeader("Content-Type", "text/plain"); //request.setBody("demo"); } catch (Exception e) { e.printStackTrace(); return; } 对请求进行签名,生成okhttp3.Request对象来访问API。 1 2 3 okhttp3.Request signedRequest = Client.signOkhttp(request); OkHttpClient client = new OkHttpClient.Builder().build(); Response response = client.newCall(signedRequest).execute();
  • 获取SDK 签名SDK只包含签名功能,不包含云服务的SDK功能,云服务SDK请参见SDK。 点此下载SDK与Demo。 解压时选择解压到当前文件夹,解压后目录结构如下: 名称 说明 hasher.cpp SDK代码 hasher.h header.h RequestParams.cpp RequestParams.h signer.cpp signer.h constants.h Makefile Makefile文件 main.cpp 示例代码