Android Question Timing Out Properly with HttpRequest and HttpJob

Steve Kirk

Member
Licensed User
Longtime User
Hi B4Android Programmers,

I've recently been working with the HttpUtils2 library to develop an application that wirelessly uploads records to a data server and also downloads text strings generated by php files on the server accessed through http.

For the most part the Basic4Android functions I've used to do this have worked as expected There are occasionally instances however where the application fails to either connect successfully or generate a connection error. Instead the application seems to just go into limbo. I believe that the connection attempt is probably timing out. I 'm simply unaware of how to properly handle timeout events in these cases.

I've included some sample code below. The first example uses HttpRequest to retrieve text data from a php file on the data server specified by a url string. The Http request initiated in the 'retrieveData' sub works properly If there is a successful response the 'Http_ResponseSuccess' procedure also functions as expected. The same is true for the 'Http_ResponseError' procedure which executes as expected when there is a connection error.

The problem is with the third scenario that also seems to occur. The http request times out while attempting to establish a connection. I imagine that the 'http request' function attempts to execute a timeout procedure. I've guessed that this procedure is something like the 'Http_ResponseTimeOut' procedure that I've shown in the code below. I haven't come across any documentation for this procedure and I'm only guessing at its name and parameters.

If a procedure like this does exist for handling http requiest timeouts I would really appreciate a block of sample code that shows the actual procedure name, its parameters and how those parameters are typically used.

I have a similar question regarding timeous and the 'http job' function. I pose this question at t the end of the following sample code.

--------------------------------------------------------------------------------------------------

Sub retrieveData

Dim HttpResponse1 As HttpResponse
Dim request As HttpRequest

Dim mUrl As String
Dim HttpResponse1 As HttpResponse

mUrl = "http://myurl.php"

mHttpResult = ""

request.InitializeGet(mUrl)

request.Timeout=3000

ProgressDialogShow("Please wait...Connecting")

If httpClient1.Execute(request, 1) = False Then
ProgressDialogHide
Msgbox("Connection Failed. Please try again later.", "Error")
End If

End Sub

--------------------------------------------------------------------------------------------------

Sub Http_ResponseSuccess (Response As HttpResponse, TaskId As Int)

ProgressDialogHide

mHttpResult = Response.GetString("UTF8")

If TaskId = 1 Then
fillUpdateTable(mHttpResult)
End I

If TaskId = 2 Then
SynchronizeFiles(mHttpResult)
End If

End Sub

---------------------------------------------------------------------------------------------------

Sub Http_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int,TaskId As Int )

ProgressDialogHide

Msgbox("Connection Error. Please try again later. " & Response.GetString("UTF8") & " " & StatusCode, "" )

If Response <> Null Then
Response.Release
End If

End Sub

----------------------------------------------------------------------------------------------------

Sub Http_ResponseTimeOut (Http_TimeOut As String)

ProgressDialogHide

Msgbox ("Timeout: " & Http_TimeOut, "Connection Time Out")

End Sub

---------------------------------------------------------------------------------------------------

I also use the 'http job' function to upload records to a server in the form of a tab-delimited string.
So far the sample code I've included below seems to work very reliably. I'm not sure however how to handle timeouts if they occur with this function. I have surmised that connection errors
are generally handled by the Job.Success method which I show in the sample 'JobDone' procedure below. If Job.Success is true, the connection and string upload was successful and the application can proceed on that basis. If Job.Success is false then the transfer failed and appropriate action can be taken. There doesn't seem to be any provision for a timeout situation with this function however. If there is indeed a way of handling timeouts with the 'http job' function? I would again really appreciate a bit of sample code showing how it is used.

Thanks in advance for any help you can give me with these questions.

Steve Kirk
---------------------------------------------------------------------------------------------------

Sub UploadRecords

Dim HttpUploadRecords As HttpJob

HttpUploadRecords.Initialize("InsertRecords", Me)

mURLString = "http://myrul2.php"


HttpUploadRecords.Download2( mURLString, Array As String("oid", pCurrOfficerID, "surveyData", mDataString))

ProgressDialogShow("Please wait...Uploading Records")

End Sub

End Sub

----------------------------------------------------------------------------------------------------

Sub JobDone (Job As HttpJob)

ProgressDialogHide

If Job.Success = True Then

If Job.JobName = "InsertRecords" Then
ToastMessageShow("Records uploaded.", True)
End If
Else
ToastMessageShow("Upload Error: " & Job.ErrorMessage, True)
End If


........

Job.Release

End Sub

---------------------------------------------------------------------------------------------------
 

DonManfred

Expert
Licensed User
Longtime User
please use code-tags when you post code... [ CODE] [ /CODE] (without the spaces in tag...)
 
Upvote 0
Top