如何从触发器调用 google 自定义搜索 Web 服务并解析响应以保存在我的 sObject 中
How to call a google custom search web service from trigger and parse the response to save in my sObject
global class CalloutSevicesQuery implements Database.Batchable<sObject>, Database.AllowsCallouts{
Public String Query;
Public String ID;
public string Body;
global CalloutSevicesQuery(String QueryID)
{
ID = QueryID;
}
global Database.QueryLocator start(Database.BatchableContext BC)
{
Query='SELECT id, BodyTxt__c,SubjectTxt__c FROM CustomEmailObj__c where id =:ID';
return Database.getQueryLocator(Query);
}
global void execute(Database.BatchableContext BC,List<CustomEmailObj__c> CusEmail)
{
String url = 'http://google.com/#q=';
for(CustomEmailObj__c C : CusEmail){
Body=C.BodyTxt__c;
System.debug('333'+Body);
}
Body= EncodingUtil.urlEncode(Body, 'UTF-8');
// build callout
url += Body;
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod('GET');
req.setTimeout(60000);
// system.debug('!2222'+req);
HttpResponse res = h.send(req);
Integer statusCode = res.getStatusCode();
// system.debug('!1111'+statusCode);
String statusName = res.getStatus();
//system.debug('!33333'+statusName);
}
global void finish(Database.BatchableContext BC)
{
}
}
我应该使用什么端点url?因为现在我得到 status 301.
我想 解析 存储在 body 变量中的字符串到 google 搜索引擎
对我来说,它适用于:
https://www.google.com
当然你也必须在远程站点设置中设置这个端点
如果我在开发者控制台中执行以下代码:
String url = 'https://www.google.com/#q=';
String body = 'hallo';
url += Body;
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod('GET');
req.setTimeout(60000);
HttpResponse res = h.send(req);
Integer statusCode = res.getStatusCode();
system.debug(statusCode);
String statusName = res.getStatus();
system.debug(statusName);
结果是:
Debug | 200
Debug | OK
global class CalloutSevicesQuery implements Database.Batchable<sObject>, Database.AllowsCallouts{
Public String Query;
Public String ID;
public string Body;
global CalloutSevicesQuery(String QueryID)
{
ID = QueryID;
}
global Database.QueryLocator start(Database.BatchableContext BC)
{
Query='SELECT id, BodyTxt__c,SubjectTxt__c FROM CustomEmailObj__c where id =:ID';
return Database.getQueryLocator(Query);
}
global void execute(Database.BatchableContext BC,List<CustomEmailObj__c> CusEmail)
{
String url = 'http://google.com/#q=';
for(CustomEmailObj__c C : CusEmail){
Body=C.BodyTxt__c;
System.debug('333'+Body);
}
Body= EncodingUtil.urlEncode(Body, 'UTF-8');
// build callout
url += Body;
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod('GET');
req.setTimeout(60000);
// system.debug('!2222'+req);
HttpResponse res = h.send(req);
Integer statusCode = res.getStatusCode();
// system.debug('!1111'+statusCode);
String statusName = res.getStatus();
//system.debug('!33333'+statusName);
}
global void finish(Database.BatchableContext BC)
{
}
}
我应该使用什么端点url?因为现在我得到 status 301.
我想 解析 存储在 body 变量中的字符串到 google 搜索引擎
对我来说,它适用于:
https://www.google.com
当然你也必须在远程站点设置中设置这个端点 如果我在开发者控制台中执行以下代码:
String url = 'https://www.google.com/#q=';
String body = 'hallo';
url += Body;
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod('GET');
req.setTimeout(60000);
HttpResponse res = h.send(req);
Integer statusCode = res.getStatusCode();
system.debug(statusCode);
String statusName = res.getStatus();
system.debug(statusName);
结果是:
Debug | 200
Debug | OK