我在 Microsoft D365 F&O 中创建了一个非常简单的表单:
系统应该像这样工作:
位置列是可以编辑的,用户应该输入一个区域,而温度列(只读)必须在用户单击按钮后根据区域显示适当的温度。
这适用于特定的 API,该 API 将区域作为参数,并使用包含详细信息的 json 文件进行回复。我用 postman 测试了 API,效果很好。
API端点:https://api.weatherapi.com/v1/current.json?key=5b6d44effc87449990c101000241405&aqi=no&**q=location**
JSON 响应示例:
{
"location": {
"name": "New York",
"region": "New York",
"country": "United States of America",
"lat": 40.71,
"lon": -74.01,
"tz_id": "America/New_York",
"localtime_epoch": 1716281298,
"localtime": "2024-05-21 4:48"
},
"current": {
"last_updated_epoch": 1716281100,
"last_updated": "2024-05-21 04:45",
"**temp_c": 15.0**,
"temp_f": 59.0,
"is_day": 0,
"condition": {
"text": "Clear",
"icon": "//cdn.weatherapi.com/weather/64x64/night/113.png",
"code": 1000
}
}
}
我想要的只是在从表单中的位置读取区域后,在 JSON 响应中填充温度值
temp_c
并将其与请求作为参数一起发送。
我尝试了这段代码,但我知道它有很多代码需要添加。
public void clicked()
{
System.Net.HttpWebRequest request;
System.IO.Stream stream;
System.Exception sysEx;
str URLAPI;
str location;
Query query;
QueryRun queryRun;
WeatherTable weatherTable;
// Define a query to retrieve the location from WeatherTable
query = new Query();
// Create a query run
queryRun = new QueryRun(query);
// Run the query and retrieve the location value
queryRun.next();
WeatherTable = queryRun.get(tableNum(WeatherTable));
location = weatherTable.Location;
// Construct the API URL
URLAPI = strFmt("https://api.weatherapi.com/v1/current.json?key=5b6d44effc87449990c101000241405&aqi=no&q=%1", location);
// Create the HTTP request
request = System.Net.WebRequest::Create(URLAPI) as System.Net.HttpWebRequest;
request.Method = 'POST';
request.ContentType = 'application/json';
// Further code for handling the HTTP request...
// Set the request headers
//System.Net.WebHeaderCollection headerCollection = request.Headers;
//headerCollection.Set('Name', 'Value');
var utf8 = System.Text.Encoding::get_UTF8();
// Set the request body
var byteArrayPayload = utf8.GetBytes("{\"key1\":\"value1\",\"key2\":\"value2\"}");
try
{
// send out the payload
using (System.IO.Stream dataStream = request.GetRequestStream())
{
dataStream.Write(byteArrayPayload, 0, byteArrayPayload.Length);
}
// request.GetResponse() may already result in an error if the request was e.g. a Bad Request(Status Code 400). This should be handled upstream via our global error handling.
using (System.Net.HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse)
{
stream = response.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader(stream);
str responseBody = reader.ReadToEnd();
}
}
catch (sysEx)
{
throw;
}
如果你想调用第三方API,我的解决方案是: