我正在尝试从API检索数据,但是即使在GET请求之前将其设置为空,我的变量也不会更新。
仅当我关闭Excel并重新打开时,变量的数据才会更新。
对此有什么解释吗?我已经挠了很久。
这是代码
int main()
{
Derived* derived = new Derived();
Base* base = (Base*) derived; //upcast
Derived* derived2 = (Derived*) base; //downcast
return 0;
}
最后,即使多次刷新F5,“ strResponse”也始终相同。我可以看到数据在网络浏览器上不再准确。我希望VBA程序在不关闭Excel的情况下获取准确的数据并刷新。
该怎么做?
答案 0 :(得分:1)
您可以添加一条指令,以免获得缓存的结果(服务器可以忽略此结果,但是我过去在此方面取得了很好的成功)。确保异步参数始终为False,并在测试之间留出更多时间。我注意到有时价格变化缓慢,因此由于间隔太短/尝试次数不足,您可能会错过价格变化。不过,您会注意到size
的变化。您应该在底部脚本的循环中添加max timeout。
还删除了匈牙利符号。
Option Explicit
Public Sub getJsonResult()
Dim http As Object
Dim urlXBTUSD As String
Dim response As String
Dim j As Long
Const ASYNC_ARG As Boolean = False
Set http = CreateObject("MSXML2.XMLHTTP")
For j = 1 To 10
response = vbNullString
urlXBTUSD = "https://www.bitmex.com/api/v1/orderBook/L2?symbol=XBTUSD&depth=3"
ThisWorkbook.Activate
With http
.Open "GET", urlXBTUSD, ASYNC_ARG
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
.send
response = .responseText
Debug.Print response
End With
Application.Wait Now + TimeSerial(0, 0, 15)
Next
End Sub
通过循环直到退货集合中的第一件商品的价格发生变化,这是一种冗长而乏味的证明方式。我使用jsonconverter.bas添加到项目中,并使用VBE>工具>参考> Microsoft脚本运行时参考。
Option Explicit
Public Sub getJsonResult()
Dim http As Object
Dim urlXBTUSD As String
Dim response As String
Dim j As Long
Const ASYNC_ARG As Boolean = False
Dim price As String, firstValue As String
Set http = CreateObject("MSXML2.XMLHTTP")
urlXBTUSD = "https://www.bitmex.com/api/v1/orderBook/L2?symbol=XBTUSD&depth=3"
ThisWorkbook.Activate
With http
.Open "GET", urlXBTUSD, ASYNC_ARG
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
.send
firstValue = JsonConverter.ParseJson(.responseText)(1)("price")
Debug.Print firstValue
Do
.Open "GET", urlXBTUSD, blnAsync
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
.send
price = JsonConverter.ParseJson(.responseText)(1)("price")
Application.Wait Now + TimeSerial(0, 0, 5)
Loop While price = firstValue
Debug.Print price
End With
End Sub
答案 1 :(得分:0)
这是出于诊断目的。
On Error Resume Next
Set File = WScript.CreateObject("Msxml2.XMLHTTP.6.0")
File.Open "GET", "https://www.google.com.au/search?q=cat", False
File.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 1.1.4322; .NET CLR 3.5.30729; .NET CLR 3.0.30618; .NET4.0C; .NET4.0E; BCD2000; BCD2000)"
File.Send
wscript.echo "=================="
wscript.echo ""
wscript.echo "Error " & err.number & "(0x" & hex(err.number) & ") " & err.description
wscript.echo "Source " & err.source
wscript.echo ""
wscript.echo "Server Response " & File.Status & " " & File.StatusText
wscript.echo File.getAllResponseHeaders
wscript.echo File.ResponseBody