尝试在 android/java 中使用 odata4j 向 odata 服务器发布信息时出现问题

发布于 2024-11-12 02:49:52 字数 4270 浏览 2 评论 0原文

我正在尝试使用 odata4j 库将 OData 发布到客户端的服务器。我通过创建自定义 CursorWrapper 来获取每列的类型来实现此目的。似乎无论我做什么,我都会收到“406 不可接受”错误。

odata4j javadocs 并不是最好的,并且 odata4j 站点以及一般谷歌搜索中都严重缺乏示例。我什至不确定如何记录发布到 OData 服务器的内容(我确信这会使错误变得清晰)。似乎没有一个明显的函数可以将 xml post 转换为字符串。

所以,我的问题是一个由两部分组成的问题: 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();                
        }
    }    

I'm attempting to use the odata4j lib to make an OData post to a client's server. I'm doing this by creating a custom CursorWrapper to get the type of each column. It seems that no matter what I do, I'm getting a '406 Not Acceptable' error.

The odata4j javadocs aren't the greatest and there is a severe lack of examples both at the odata4j site as well as from general google searches. I'm not even sure how to log what is being posted to the OData server (I'm sure that would make the error clear). There doesn't seem to be an obvious function to get the xml post to a string.

So, my question is a 2 part question:
1. How do you log the the transactions from the odata4j lib?
2. What, if anything, is wrong in my logic to make an OData post using odata4j?

I'm including a code snippet below. Any help would be greatly appreciated.

    // 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();                
        }
    }    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烧了回忆取暖 2024-11-19 02:49:52

要记录所有 http 流量:

ODataConsumer.dump.all(true);

请告诉我您发现了什么。

希望有帮助,
- 约翰

To log all http traffic:

ODataConsumer.dump.all(true);

Let me know what you find out.

Hope that helps,
- john

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文