【问题标题】:PUT HTTP Adapter in MobileFirst Platform在 MobileFirst 平台中放置 HTTP 适配器
【发布时间】:2015-04-17 15:17:19
【问题描述】:

我正在尝试从我的原生 android 应用程序中发布一些数据。

本机代码:

WLProcedureInvocationData invocationData = new WLProcedureInvocationData("TaskAdapter", "updateTask");

int taskId = Integer.parseInt(tvTaskId.getText().toString());

String assignedTo = tvAssignedTo.getText().toString();

String address = "";

String description = "";

String latitude = "5.0";

String longitude = "5.0";

String status = "5.0";
String comments = "5.0";
String lastupdate = "5.0";
String userLatitude = "5.0";
String userLongitude = "5.0";
String userLocation = "5.0";
String photoData = "5.0";

Object[] parameters = new Object[]{
    taskId,
    assignedTo,
    description,
    address,
    latitude,
    longitude,
    status,
    comments,
    lastupdate,
    userLatitude,
    userLongitude,
    userLocation,
    photoData
};

invocationData.setParameters(parameters);

WLRequestOptions options = new WLRequestOptions();
options.setTimeout(30000);


client.getInstance().invokeProcedure(invocationData, new MyInvokeListener(), options);

适配器代码:

function updateTask(id) {   
    var input = {
        method : 'PUT',
        returnedContentType : 'json',
        path : '/Api/Task?taskid=' + id
    };


    return WL.Server.invokeHttp(input);
}

适配器 XML:

<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed Materials - Property of IBM
5725-I43 (C) Copyright IBM Corp. 2011, 2013. All Rights Reserved.
US Government Users Restricted Rights - Use, duplication or
disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
-->
<wl:adapter name="TaskAdapter" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:wl="http://www.ibm.com/mfp/integration" xmlns:http="http://www.ibm.com/mfp/integration/http">

    <displayName>TaskAdapter</displayName>
    <description>TaskAdapter</description>
    <connectivity>
        <connectionPolicy xsi:type="http:HTTPConnectionPolicyType">
            <protocol>http</protocol>
            <domain>testmeternative.vdot.virginia.gov</domain>
            <port>80</port>
            <connectionTimeoutInMilliseconds>30000</connectionTimeoutInMilliseconds>
            <socketTimeoutInMilliseconds>30000</socketTimeoutInMilliseconds>
            <maxConcurrentConnectionsPerNode>50</maxConcurrentConnectionsPerNode>

            <!-- Following properties used by adapter's key manager for choosing specific 
                certificate from key store <sslCertificateAlias></sslCertificateAlias> <sslCertificatePassword></sslCertificatePassword> -->
        </connectionPolicy>
    </connectivity>

    <procedure name="getAllTasks" />
    <procedure name="updateTask" />


</wl:adapter>

我不确定我是否以正确的方式发送正文。此外,如何将id(参数)发送到适配器函数。

当我在 Eclipse 中单击 Call Mobile First Adapter 时,它会向我显示过程名称,但下拉菜单中的 REST 调用类型为 GET,我希望它为 PUT

【问题讨论】:

  • 你的问题很混乱。 1)如果要使用“put”,那么问题摘要中的“post”是什么意思? 2)参数对象中的空“”是什么? 3)什么是“id”?
  • 嗨 Idan,更正了我的更改。参数对象中的空 "" 是我正在为地址传递一个空字符串。我想在查询字符串中传递 taskid 并将任务对象作为 body 传递。所以我想创建一个适配器函数,它将参数 id 作为任务 id
  • 您问题的 Eclipse 部分可能取决于适配器 XML,请告诉我们。乍一看,您传递的任务 ID 很好,您应该能够将任何原始类型作为参数传递。但是,您传递了许多参数,但只希望收到一个。
  • 是的,结果只会是成功或失败字符串,它是一种后端测试方法。也添加了我的 XML
  • @user3681378 您使用的是哪个版本的 MobileFirst(6.3 或 7.0)?

标签: ibm-mobilefirst mobilefirst-adapters mobilefirst-server


【解决方案1】:

您需要按如下方式更新您的适配器代码:

function updateTask(id, assignedTo, description, address, latitude, longitude,
        status, comments, lastupdate, userLatitude, userLongitude,
        userLocation, photoData) {

    var data = {
        "assignedTo" : assignedTo,
        "description" : description,
        "address" : address,
        "latitude" : latitude,
        "longitude" : longitude,
        "status" : status,
        "comments" : comments,
        "lastupdate" : lastupdate,
        "userLatitude" : userLongitude,
        "userLocation" : userLocation,
        "photoData" : photoData
    };

    var input = {
        method : 'PUT',
        returnedContentType : 'json',
        path : '/Api/Task?taskid=' + id,
        body : {
            contentType : 'application/json',
            content : data
        }
    };

    return WL.Server.invokeHttp(input);
}

由于您在本机代码中通过 invocationData.setParameters(parameters); 将值传递给适配器,这意味着适配器将以相同的顺序获取相同数量的参数。

我创建了一个对象data,它将包含除idtaskId 之外的所有这些值,因为您将它作为查询参数传递。然后我假设您的后端服务接受Content-Typeapplication/json,如果需要,您可以更改内容类型。

【讨论】:

    【解决方案2】:

    确保区分您的应用如何调用适配器和适配器如何调用后端 - 这些是不同的概念。

    在早期版本的 MFP/Worklight 适配器中,使用 HTTP GET 调用;然后适配器本身可能会使用 GET、PUT 或 POST 调用后端,但应用程序实际上是通过 HTTP 进行 RPC 调用。

    使用 MFP 7.0 版,我们获得了创建 RESTful 适配器的能力,可以使用 GET、PUT、POST 或 DELETE 调用该适配器。这些适配器是使用 JAX/RS 编程模型在 Java 中实现的。每个单独的过程都将被标记为使用这些 HTTP“动词”之一,然后当您在 Eclipse 中进行测试时,当您选择该过程时,将提供适当的 GET/PUT/POST。在您的示例中,您有一个简单的传统 JavaScript 适配器,因此只能使用 GET,而这正是测试工具所提供的。

    要调用 Java RESTful 适配器,您需要指定动词。 See this tutorial:

    了解如何创建 Java RESTful 适配器see this tutorial

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多