ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

从Android连接到Azure DocumentDB

2019-05-28 14:13:52  阅读:201  来源: 互联网

标签:android azure azure-cosmosdb


有没有人经历过从android连接到Azure的documentdb?我尝试过使用新的Java SDK,但由于核心android SDK中存在一些旧的必需Apache依赖冲突,因此它似乎在android中不受支持.我正在尝试Rest API方法,但它很慢.

在此先感谢您的帮助!

解决方法:

好吧,这是一个要解决的噩梦.九个小时后…… -_-‘.简而言之,这里有一些实际上可以成功运行的代码.此代码并不完美,依赖于Retrofit

首先,这是Retrofit中的一个“服务”界面示例:

import retrofit.Callback;
import retrofit.http.Body;
import retrofit.http.Header;
import retrofit.http.POST;

public interface MyPojoService {
    @POST("/dbs/[db_id]/colls/[collection_id]/docs")
    void addDocument(@Header("authorization") String authorization, @Header("x-ms-date") String date, @Body MyPojo myPojo, Callback<MyPojo> cb);
}

接下来,我们在类中将初始设置字段调用DocumentDB:

// Replace with your DocumentDB master key.
private static final String MASTER_KEY = "[Insert Key Here]";

//Gson instance.
private Gson gson = new Gson();

现在我们将拥有针对RESTful端点执行的方法:

public void callDocumentDB() {
    SimpleDateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
    formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
    String headerDate = formatter.format(new Date()).toLowerCase();  //According to the spec the format matters here.  Make sure to use this format on the header dates.


    MyPojo myPojo = new MyPojo();
    myPojo.id = UUID.randomUUID().toString(); //This is the only required field, and does not have to be a UUID.

    RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint("https://[INSERT DB NAME HERE].documents.azure.com")
            .build();

    MyPojoService service = restAdapter.create(MyPojoService.class);

    service.addDocument(generateAuthHeader("post", "docs", "[INSERT COLLECTION ID HERE]", headerDate, MASTER_KEY), headerDate, myPojo, new Callback<MyPojo>() {
        @Override
        public void success(MyPojo myPojo, Response response) {
            //[INSERT API SUCCESSFUL CALL LOGIC HERE]
        }

        @Override
        public void failure(RetrofitError error) {
            throw error;
        }
    });
}

最后,我们有生成授权标头的方法.这种方法是一个整合的噩梦,但它适用于规范:

private String generateAuthHeader(String verb, String resourceType, String resourceId, String date, String masterKeyBase64) throws Exception
{
    //Decode the master key, and setup the MAC object for signing.
    byte[] masterKeyBytes = Base64.decode(masterKeyBase64, Base64.NO_WRAP);
    Mac mac = Mac.getInstance("HMACSHA256");
    mac.init(new SecretKeySpec(masterKeyBytes, "HMACSHA256"));

    //Build the unsigned auth string.
    String stringToSign = verb + "\n"
            + resourceType + "\n"
            + resourceId + "\n"
            + date + "\n"
            + "\n";

    //Sign and encode the auth string.
    String signature = Base64.encodeToString(mac.doFinal(stringToSign.toLowerCase().getBytes("UTF8")), Base64.NO_WRAP);

    //Generate the auth header.
    String authHeader = URLEncoder.encode("type=master&ver=1.0&sig=" + signature, "UTF8");

    return authHeader;
}

注意:请注意,generateAuthString和MyPojoService设置为使用x-ms-date标头而不是日期标头.当前版本的Azure的DocumentDB存在一个错误,似乎是保持生成的令牌无法正确授权.

我希望这会有所帮助,并为您节省一些时间.

标签:android,azure,azure-cosmosdb
来源: https://codeday.me/bug/20190528/1171385.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有