0

我正在尝试连接到 Azure 数据市场,它是一个 odata 存储库。我正在使用最新的 Olingo 库 r4.2.0。以下代码:

String serviceUrl = "https://api.datamarket.azure.com/DataGovUK/MetOfficeWeatherOpenData/v1/";

ODataClient client = ODataClientFactory.getClient();
ODataServiceDocumentRequest req = client.getRetrieveRequestFactory().getServiceDocumentRequest(serviceUrl);
req.setAccept("application/json;application/xml;odata.metadata=full");
req.setContentType("application/json;application/xml;odata.metadata=full");
ODataRetrieveResponse res = req.execute();

返回异常

 org.apache.olingo.client.api.communication.ODataClientErrorException: null [HTTP/1.1 415 Unsupported Media Type]

服务器返回:

<?xml version="1.0" encoding="utf-8"?>
<m:error mlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
   <m:code />
   <m:message xml:lang="en-US">
       Unsupported media type requested.
   </m:message>
</m:error>

有人试图用这个库或另一个用 Java 连接到 Azure 数据市场?

4

1 回答 1

2

尝试设置请求标头Accept&Content-Type仅使用JSONor XML,而不是两者。请看下文。

req.setAccept("application/json");
req.setContentType("application/json;odata.metadata=full");

或者

req.setAccept("application/atom+xml,application/xml");
req.setContentType("application/atom+xml,application/xml;odata.metadata=full");

和授权

req.addCustomHeader("Authorization", "Basic " + getAccountKey());

其他stackoverflow帖子中描述的帐户密钥是:

public String getAccountKey()
{
    String accountKey = "My Microsoft Azure Account Key";
    byte[] accountKeyBytes = Base64.encodeBase64((accountKey + ":" + accountKey).getBytes());
    String accountKeyEnc = new String(accountKeyBytes);
    return accountKeyEnc;
}
于 2016-06-27T05:59:19.313 回答