问题
I am using the below code, to get data from API and add data to table. I want to show the users the progress of the API Request Made. Hence using If to get the status of the request using readyState Property (IXMLHTTPRequest)
Problem -
When this function is called, I just get one msgbox with 4. What am I missing??
Option Compare Database
Dim ApiUrl As String
Dim reader As New XMLHTTP60
Dim coll As Collection
Dim Json As New clsJSONParser
Public Sub ApiInitalisation()
ApiUrl = "http://private-anon-73376961e-countingappapi.apiary-mock.com/"
End Sub
Public Sub GetPerson()
On Error GoTo cmdLogIn_Click_Err
'For API
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim contact As Variant
Api.ApiInitalisation
ApiUrl = ApiUrl & "users/5428a72c86abcdee98b7e359"
reader.Open "GET", ApiUrl, False
'reader.setRequestHeader "Accept", "application/json"
reader.send
'Temporay variable to store the response
Dim egTran As String
Relevant Code Start
If reader.ReadyState = 0 Then
MsgBox (0)
End If
If reader.ReadyState = 1 Then
MsgBox (1)
End If
If reader.ReadyState = 2 Then
MsgBox (2)
End If
If reader.ReadyState = 3 Then
MsgBox (3)
End If
If reader.ReadyState = 4 Then
MsgBox (4)
End If
'-------------
' Why is this code required?
' Not yet found answer
'Do Until reader.ReadyState = 4
' DoEvents
'Loop
'-------------------
Relevant Code End
' Add data to Table
If reader.Status = 200 Then
Set db = CurrentDb
Set rs = db.OpenRecordset("tblPerson", dbOpenDynaset, dbSeeChanges)
egTran = "[" & reader.responseText & "]"
Set coll = Json.parse(egTran)
For Each contact In coll
rs.AddNew
rs!FName = contact.Item("name")
rs!Mobile = contact.Item("phoneNumber")
rs!UserID = contact.Item("deviceId")
rs!SID = contact.Item("_id")
rs.Update
Next
Else
MsgBox "Unable to import data."
End If
End Sub
As per Microsoft documentation
0 (UNINITIALIZED) =
The object has been created, but not initialized (the open method has not been called).
(1) LOADING =
The object has been created, but the send method has not been called.
(2) LOADED =
The send method has been called, but the status and headers are not yet available.
(3) INTERACTIVE =
Some data has been received. Calling the responseBody and responseText properties at this state to obtain partial results will return an error, because status and response headers are not fully available.
(4) COMPLETED =
All the data has been received, and the complete data is available in the responseBody and responseText properties.
回答1:
Unless you're missing some code or I'm misunderstanding, you're already using reader.send prior to checking the ReadyState. It's always going to come back as Completed, unless it encounters a fatal error (and even then it may come back as Completed).
来源:https://stackoverflow.com/questions/35595437/show-progress-of-http-request-in-ms-access