我正在尝试使用 odata4j 库将 OData 发布到客户端的服务器。我通过创建自定义 CursorWrapper 来获取每列的类型来做到这一点。似乎无论我做什么,我都会收到“406 Not Acceptable”错误。
odata4j javadocs 不是最好的,并且在 odata4j 站点和一般的 google 搜索中都严重缺乏示例。我什至不确定如何记录发布到 OData 服务器的内容(我确信这会使错误变得清晰)。似乎没有一个明显的功能可以将 xml 帖子转换为字符串。
所以,我的问题是一个两部分的问题: 1. 你如何记录来自 odata4j 库的事务?2. 如果有的话,我使用 odata4j 发布 OData 帖子的逻辑有什么问题?
我在下面包含一个代码片段。任何帮助将不胜感激。
// Create the ODataConsumer with the appropriate credentials
OClientBehavior basicAuth = new BasicAuthenticationBehavior(Config.dbfile +
"\\" + Config.username, Config.password);
ODataConsumer consumer = ODataConsumer.create(url, basicAuth);
// Make sure there are results in the cursor
if ( cursorWrapper.moveToFirst() ){
// create the new product
OCreateRequest<OEntity> newMaterial =
consumer.createEntity( "ESvcOrderTrans" );
// Iterate through each cursor's row
while (cursorWrapper.isAfterLast() == false) {
// Iterate through each cursor's columns
for ( int i=1; i < cursorWrapper.getColumnCount(); i++ ){
// Determine type of key
switch ( cursorWrapper.getType(i) ){
case CustomCursorWrapper.FIELD_TYPE_INTEGER :
if (cursorWrapper.isNull(i)){
createRequest.properties(OProperties.null_(
cursorWrapper.getColumnName(i),
"Edm.Int32"));
} else {
createRequest.properties( OProperties.int32(
cursorWrapper.getColumnName(i),
cursorWrapper.getInt(i)));
}
break;
case CustomCursorWrapper.FIELD_TYPE_STRING :
if (cursorWrapper.isNull(i)){
createRequest.properties(OProperties.null_(
cursorWrapper.getColumnName(i),
"Edm.String"));
} else {
createRequest.properties(OProperties.string(
cursorWrapper.getColumnName(i),
cursorWrapper.getString(i)));
}
break;
case CustomCursorWrapper.FIELD_TYPE_FLOAT :
if (cursorWrapper.isNull(i)){
createRequest.properties(OProperties.null_(
cursorWrapper.getColumnName(i),
"Edm.Double"));
} else {
createRequest.properties(OProperties.decimal(
cursorWrapper.getColumnName(i),
cursorWrapper.getFloat(i)));
}
break;
case CustomCursorWrapper.FIELD_TYPE_BLOB :
if (cursorWrapper.isNull(i)){
createRequest.properties(OProperties.null_(
cursorWrapper.getColumnName(i),
"Edm.Binary"));
} else {
createRequest.properties(OProperties.binary(
cursorWrapper.getColumnName(i),
cursorWrapper.getBlob(i)));
}
break;
case CustomCursorWrapper.FIELD_TYPE_NULL :
break;
}
}
// Execute the OData post
newMaterial.execute();
// Move to the next cursor
cursorWrapper.moveToNext();
}
}