Salesforce Apex页面中调用远端网络服务

本文介绍了Salesforce Apex页面中调用远端网络服务的实现过程。

注册远端网络服务

在使用Apex代码调用远端网络服务之前,首先需要在Salesforce中注册远端网络服务地址, 本文使用librarything.com提供的一个API, 输入为ISBN, 返回其对应的语言。

登录Salesforce后,选择Setup > Administer > Security Controls > Remote Site Settings

1076449-20161214174825073-420621308.png

然后输入远端网络服务地址并保存

1076449-20161214174843479-459668251.png

实现Apex Controller

Controller代码如下, isbn用于接收UI的输入, language向UI返回API的输出。

checkLanguage方法实现API的调用。

    public class BookController {
        public String isbn { get; set; }
        public String language { get; set; }
        
        public void checkLanguage() {
            
            Http http = new Http();
            
            String url = 'http://www.librarything.com/api/thingLang.php?isbn=' + isbn;
            HttpRequest req = new HttpRequest();
            req.setEndpoint(url);
            req.setMethod('GET');
            
            HttpResponse res = http.send(req);
            language = res.getBody();
        }
    }

Apex页面

页面代码如下

    <apex:page controller="BookController" showChat="false" showHeader="false">
        <apex:form >
            ISBN : <apex:inputText value="{! isbn}" />
            <apex:commandButton action="{! checkLanguage}" value="Check Language" reRender="result"/>
        </apex:form> 

        Language : <apex:outputText id="result" value="{! language}"/>
    </apex:page>

转载于:https://www.cnblogs.com/huyouhengsf/p/6180293.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值