Android Question Unfortunately App has stopped HTTP Request with no data connection

Derek Jee

Active Member
Licensed User
Longtime User
Hello

I am testing my app to see what happens if I am making an http request and the data connection fails. Simply I turn off the wifi connection and run the process. When I run the request with no wifi I get the Unfortunately app has stopped message. I have put a try/end try around the code to see where it errors but it does not trap it. I am running in release mode too when I turn off my wifi. Of course I am checking for a connection before I run this process but I want to make sure the app doesn't crash if connection fails after the check..

Can anyone advise?

Many thanks,


Derek.
 

sorex

Expert
Licensed User
Longtime User
are you placing the try/end try around the download .init and .download or in the jobdone sub?
 
Upvote 0

Derek Jee

Active Member
Licensed User
Longtime User
I have it round the post/put request code and the jobdone..
 
Upvote 0

Derek Jee

Active Member
Licensed User
Longtime User
B4X:
    Try
   
    'Log("JobName = " & Job.JobName & ", Success = " & Job.Tag & ", Error = " & Job.ErrorMessage & ", Response Body = " & Job.GetString)
    Dim x As String = Job.Tag
   
    Select x
   
        Case 200

            Dim Headers As Map= Job.HTag
            Dim y As String = Headers.Get("Authorization")

            SQL1.ExecNonQuery("UPDATE Parameters Set strUserCredentials = '" & HashedCredentials & "', strToken = '" & y & "', strUserID = '" & txtUsername.Text & "' WHERE id = 1")
            Job.Release
            StartActivity("HomePage")
               
        Case 401
            pnlInfo.Visible = True
            lblInfo.Text = "Incorrect Username Or Password Entered"
           
        Case 403
            pnlInfo.Visible = True
            lblInfo.Text = "User Is Unauthorised.  Please Contact Your Administrator"
       
    End Select
   
       Job.Release
   
    Catch
   
        Log("No Connection")
        ToastMessageShow("Try Local Logon", True)
   
    End Try
 
Upvote 0

Derek Jee

Active Member
Licensed User
Longtime User
I have a series of toast messages and it does not get to the jobdone sub. It runs the request code ok. I guess it is the httputils2 or httpjob which is unhappy?

(Sorry, yes I have a check too for Jobsucess)
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
try this and see what happends

B4X:
Sub Process_Globals
End Sub

Sub Globals
Dim dljob As HttpJob
End Sub

Sub Activity_Create(FirstTime As Boolean)
dljob.Initialize("test",Me)
dljob.Download("http://www.google.com")
End Sub

Sub JobDone(job As HttpJob)
Log("job done")
   If job.Success Then
       Log(job.GetString)
   Else
     Log("Error: " & job.ErrorMessage)
   End If
   job.Release
End Sub


when I turn Wifi off I get (without a crash)

B4X:
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
job done
Error: java.net.UnknownHostException: www.google.com
 
Upvote 0

Derek Jee

Active Member
Licensed User
Longtime User
Hi Sorex

I get the same error but no logs as I am not connected to the wifi..
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
that's strange, it should always write something to the log if the download executes.

is that on a real device or some emulator?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Sub Globals
Dim dljob As HttpJob
End Sub

Sub Activity_Create(FirstTime As Boolean)
dljob.Initialize(
"test",Me)
dljob.Download(
"http://www.google.com")
End Sub

Not a good idea to use the job in globals! You should DIM a NEW httpjob each time!
B4X:
Sub Globals
'Dim dljob As HttpJob
End Sub

Sub Activity_Create(FirstTime As Boolean)
Dim dljob As HttpJob
dljob.Initialize("test",Me)
dljob.Download("http://www.google.com")
End Sub
 
Upvote 0

Derek Jee

Active Member
Licensed User
Longtime User
Hi Sorex

It is a device.. Would it log anything if it had no connection?
 
Upvote 0

Derek Jee

Active Member
Licensed User
Longtime User
But if I have no wifi how can it log the job done? I have done this in release and legacy mode with the same problem.. I am worried that my app will just die and I can't trap the error. I will have to look at the HTTPutils2 to see if that is what is erroring..
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
yes, the job should always complete but it will be unsuccessfull (job.success = false)
 
Upvote 0

Derek Jee

Active Member
Licensed User
Longtime User
I though it did.. So if I send a request with no data connection at all it should still fire the jobdone sub but with success = false?

I am using HTTPUtils2 v2.01..
 
Upvote 0

Derek Jee

Active Member
Licensed User
Longtime User
Well... I think I have found the issue..

When I first installed the HTTPUtils2 I needed to receive the statuscode of an unsuccessful request so was advised to put in the following code to the hc_errorresponse sub.. Of course this errors when there is no connection because there is no tag.... I have trapped that code and it seems to have done the trick..

I must say a big thank you to you and sorry to have wasted your time. Thank you..

B4X:
    Dim job As HttpJob = TaskIdToJob.Get(TaskId)
    job.Tag = Response.StatusCode
 
Upvote 0
Top