Android Question OkHttp Simple Web Service Example with Cancel Job

phukol

Active Member
Licensed User
Longtime User
Hi guys, i have tried using okhttp for a project of mine using ImageDownloader and Web Service that returns parse strings However, what i want to do is to cancel the data returned by the web service when i press a button or it is has been running for too long. I have already tried studying https://www.b4x.com/android/forum/threads/download-huge-files-with-httputils2.30220/#content
but i can seem to find a way on where to start. Here is my code for the web service

B4X:
#Region  Service Attributes
    #StartAtBoot: False
   
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim parser As SaxParser
    Dim strKey As String
    
    Dim intTick As Int
End Sub

Sub Service_Create

End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_Destroy

End Sub

Sub consumeService(strData As  String)
    Dim response() As String
    response = Regex.Split(",",strData)
    Dim PostUrl As String = ""
    Dim strXML As String=""
    Dim job1 As HttpJob
    
    strProcess = response(0)
    intTick = 0
    
    If myTimer.IsInitialized = False Then myTimer.Initialize("myTimer",1000)
    myTimer.Enabled = True
         
    PostUrl = "http://xxx.php"                        
    
    
            
            strXML = "<?xml version='1.0' encoding='UTF-8'?>"
            strXML = strXML & "<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' xmlns:ns1='uri:gophil.app' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:SOAP-ENC='http://schemas.xmlsoap.org/soap/encoding/' SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>"
            strXML = strXML & "<SOAP-ENV:Body>" 
            strXML = strXML & "<ns1:getmovielist>" 
           
            strXML = strXML & "<opt xsi:type='xsd:string'>" & response(1)  & "</opt>" 
            strXML = strXML & "<st xsi:type='xsd:string'>" & response(2) & "</st>"
           
            strXML = strXML & "</ns1:getmovielist>"
            strXML = strXML & "</SOAP-ENV:Body>"
            strXML = strXML & "</SOAP-ENV:Envelope>" 
            strXML = strXML.Replace("'", Chr(34))
           
            'Log(strXML)
            job1.Initialize("getMovieList", Me)
            job1.PostString(PostUrl, strXML)
            job1.GetRequest.SetHeader("getMovieList", PostUrl)
            job1.GetRequest.SetContentType("text/xml; charset=utf-8")
       
        
    
        
    
    
 
End Sub

Sub JobDone (Job As HTTPJob)
    If Job.Success Then
            Select Job.JobName
                Case "getMovieList"
                    strKey = ""
                    parser.Initialize()
                    parser.Parse(Job.GetInputStream , "Parser") 
            End Select
            
    Else
        
    End If
    Job.Release
   
        
            
End Sub

Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder) 
    
   
    If   (parser.Parents.IndexOf("getmovielistResponse") > -1)  Then '
        If parser.Parents.IndexOf("item") > -1 Then
            If Name = "key" Then
                strKey = Text.ToString
            Else If Name = "value" Then
                If strKey = "title_" Then
                    strTitle = Text.ToString 
                    CallSubDelayed2(Movie_Info, "setSchedList",strTitle) 
                    
                End If 
            End If
        Else 'no data
           
        End If
    End If 
   
End Sub


Sub myTimer_Tick
    intTick = intTick + 1
    If intTick = 12  Then
    
        intTick = 0
        myTimer.Enabled = False
        StopService(Me)
    End If
End Sub

and for my imagedownloader
B4X:
#Region  Service Attributes
    #StartAtBoot: False
#End Region

Sub Process_Globals
    Private cache As Map
    Private tasks As Map
    Private ongoingTasks As Map
    Public bitProgress,bitRounded  As Boolean
    Dim rsie As RSImageEffects
    Dim iu As AS_ImageUtils
    Public intForm As Int
    Private const ADD_PROGRESSBAR As Boolean = True
    Dim p  As MaterialCircleProgress
   
     
End Sub

Sub Service_Create
    tasks.Initialize
    cache.Initialize
    ongoingTasks.Initialize
    
       
End Sub

Sub Service_Start (StartingIntent As Intent)
End Sub

Sub Service_Destroy
End Sub

Sub Download (ImageViewsMap As Map)
    
    For Each iv As ImageView In ImageViewsMap.Keys '    For i = 0 To ImageViewsMap.Size - 1
        'tasks.Put(ImageViewsMap.GetKeyAt(i), ImageViewsMap.GetValueAt(i))
        'Dim link As String = ImageViewsMap.GetValueAt(i)
        Dim link As String = ImageViewsMap.Get(iv)
        Dim o As Object = iv
        tasks.Put(o, link)
       
        If cache.ContainsKey(link) Then
            'Dim iv As ImageView = ImageViewsMap.GetKeyAt(i)
            iv.SetBackgroundImage( iu.CreateScaledBitmap( iu.ReduceColors( cache.Get(link) ) ,iv.Width,iv.Height,True) )
        Else If ongoingTasks.ContainsKey(link) = False Then
            ongoingTasks.Put(link, "")
            Dim j As HTTPJob2
            j.Initialize(link, Me)
            j.Download(link)
           
            If ADD_PROGRESSBAR Then
                p.Initialize("")
                p.Colors = Array As Int(Colors.Red,Colors.Blue,Colors.Green,Colors.Yellow)
                p.ProgressStrokeWidth = 5dip
                p.ShowArrow = False 
                p.TextColor = Colors.LightGray
                p.CircleBackgroundEnabled = False
                
                Dim jo As JavaObject = iv
                Dim parent As Panel = jo.RunMethod("getParent", Null)
                iv.Tag = p
                parent.AddView(p, iv.Left, iv.Top, iv.Width, iv.Height)
            End If
        End If
    Next
    
End Sub

Sub JobDone(job As HTTPJob)
           
        
    ongoingTasks.Remove(job.JobName)
    If job.Success Then
        Try
            If tasks.IsInitialized Then
                Starter.bmp1 = iu.ReduceColors(job.GetBitmap)
                cache.Put(job.JobName, Starter.bmp1)
               
                For i = 0 To tasks.Size - 1
                    Dim link As String = tasks.GetValueAt(i)
                    If link = job.JobName Then
                        Dim iv As ImageView = tasks.GetKeyAt(i)
                        If bitRounded Then
                            Starter.bmp2 =  iu.CreateScaledBitmap( Starter.bmp1 ,iv.width, iv.height,True) 
                            iv.Bitmap = rsie.RoundCorner(Starter.bmp2, 10)
                        Else
                            iv.SetBackgroundImage(iu.CreateScaledBitmap(Starter.bmp1, iv.Width,iv.height, True) )
                        End If
                       
                        If ADD_PROGRESSBAR And iv.Tag Is MaterialCircleProgress Then
                            Dim p As MaterialCircleProgress = iv.Tag
                            p.Visible = False
                            p.RemoveView
                        End If
                    End If
                Next
            End If
        '    Starter.bmp1 = Null
        Catch
            bitProgress = False
            job.Release  
        End Try
           
    Else    
        cache.Clear   
        tasks.Clear  
    End If
    job.Release
   
    If ongoingTasks.Size = 0 Then
        tasks.Clear   
        cache.Clear   
        bitRounded = False   
        StopService("imagedownloader")
    End If
   
    bitProgress = False
End Sub

Sub ActivityIsPaused 
    tasks.Clear    
End Sub

public Sub ClearCache
   
        
        cache.Clear
        bitRounded = False    
     
End Sub

Can anyway give me a hint on how i can call them to my main modules and cancel either the web service or image downloader when i want to
 
Top