尝试连接到datamarket会返回异常

时间:2016-06-24 12:37:04

标签: java azure odata olingo

我正在尝试连接到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数据市场吗?

1 个答案:

答案 0 :(得分:2)

尝试设置请求标头Accept&amp; Content-Type只有JSONXML,而不是两者。请参阅下文。

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");

和授权<​​/ p>

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

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

public String getAccountKey()
{
    String accountKey = "My Microsoft Azure Account Key";
    byte[] accountKeyBytes = Base64.encodeBase64((accountKey + ":" + accountKey).getBytes());
    String accountKeyEnc = new String(accountKeyBytes);
    return accountKeyEnc;
}