HTTP problem

nico78

Active Member
Licensed User
Longtime User
I have a problem with the HTTP library, when I leave the program Lorqui (HC_ResponseSuccess) is called and I returned to the program, ily has a pending event (Response_StreamFinish) but I do not deal because taskid <> a, but the next call I get an error to (canvas1.DrawBitmap (image, null, destRect)) while (taskid = a)

I do not understand why it does not work, can you help me!
-----------------------------------------------------------------------------------------
In French:J'ai un problème avec la librairie HTTP, lorsque je quitte le programme lorque (HC_ResponseSuccess) est appellé et que je retourne au programme, ily a un event en attente (Response_StreamFinish) mais je ne le traite pas car taskid<>a, mais lors du prochain appel j'ai une erreur à( canvas1.DrawBitmap(image,Null,destrect)) alors que (taskid=a)

Je ne comprend pas pourquoi cela ne marche pas, pouvez vous m'aider!
-----------------------------------------------------------------------------------------

B4X:
Error Log:
Success21


play


StreamFinish succes21


CallServer22


Success22


play


StreamFinish succes22


CallServer23


** Activity (main) Pause, UserClosed = true **


sending message to waiting queue.


** Activity (main) Create, isFirst = false **


running waiting messages (1)


** Activity (main) Resume **


CallServer25
Success25


play


StreamFinish succes25


main_response_streamfinish (B4A line: 89)


canvas1.DrawBitmap(image,Null,destrect)

java.lang.NullPointerException

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim hc As HttpClient
   Dim target As OutputStream
   Dim a As Int
   Dim largeur As Int
   Dim hauteur As Int 
   Dim message As String
   
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim stop As Button
   Dim save As Button
   Dim Canvas1 As Canvas
End Sub

Sub Activity_Create(FirstTime As Boolean)

   activity.LoadLayout("essai")

   If FirstTime Then
      
        hc.Initialize("hc")
      largeur=activity.Width
      hauteur=activity.Height
      canvas1.Initialize(activity)
      End If
   
   message="play"

End Sub

Sub Activity_Resume
   If stop.Text="Stop" Then
      code.CallServer
   End If 
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   a=a+1
   If userclosed Then 
      activity.Finish
   End If 
      
End Sub

Sub HC_ResponseSuccess (Response As HttpResponse, TaskId As Int)

   If taskid=a Then
   
      If message="play" Then
      Log("Success"&TaskId)
         Log("play")
         Target = File.OpenOutput(File.DirInternalCache, "image.jpg", False)
         Response.GetAsynchronously("Response", Target, True, TaskId)
      End If 

   End If 
End Sub 

Sub HC_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
   urldone=1
   Log("erreur")
   Log(reason)
   Log("ResponseError"&TaskId)
   code.CallServer
End Sub

Sub Response_StreamFinish (Success As Boolean, TaskId As Int)

   If taskid=a Then
      If Success = False Then
         ToastMessageShow("Error downloading file: " & LastException.Message, True)
         Log("StreamFinish erreur"&TaskId)
      Else If message="play" Then
         Log("StreamFinish succes"&TaskId)
         
         Dim image As Bitmap
         image=LoadBitmap(File.DirInternalCache, "image.jpg")
         If image.IsInitialized Then
            'activity.SetBackgroundImage(image)
            Dim destrect As Rect
            destrect.Initialize(0,0,largeur,hauteur)
            canvas1.DrawBitmap(image,Null,destrect)
            activity.Invalidate
         End If
         
      End If

      code.CallServer
   End If 
End Sub

Sub Stop_Up
   If stop.Text="Stop" Then
      message="stop"
      stop.Text="Play"
      save.Visible=True
   Else
      message="play"
      stop.Text="Stop"
      save.Visible=False
      code.CallServer
   End If 
End Sub

Sub Save_Up
   
End Sub

Sub Activity_LongClick
   
End Sub

Sub Activity_Click
   If stop.Visible=False Then
      stop.Visible=True
      If stop.Text="Play" Then
         save.Visible=True
      Else
         save.Visible=False
      End If 
   Else
      stop.Visible=False
      save.Visible=False
   End If 
End Sub

B4X:
'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub


Sub CallServer
   Dim EOL As String
   EOL=Chr(13)& Chr(10)
   
   main.a=main.a+1

   postData = main.largeur & EOL & main.hauteur
   PostUrl = "http://89.92.252.152"

   Dim str() As Byte
   
   str = postData.GetBytes("UTF8")
   
    Dim req As HttpRequest
   req.InitializePost2(PostUrl, str)
   req.SetContentType("application/x-www-form-urlencoded")
   req.SetContentEncoding("UTF8Encoding")
   'req.SetHeader("popo","pipi")
   'req.Timeout=45000

   'req.InitializeGet(PostUrl)

    Main.hc.Execute(req, main.a)
   Log("CallServer"&main.a)
End Sub
 

nico78

Active Member
Licensed User
Longtime User
the problem is that canvas1.DrawBitmap (image, null, destRect) crash while streamfinish is successful, and also that the image is verified with image.initialized if I stop the application and I start when the line strem_succes is called
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Maybe it is getting destroyed.
Spot on! As an Activity object that can only be declared as a Global and not a ProcessGlobal a Canvas is destroyed together with the old Activity and needs to be recreated. Only objects in Process_Globals need to initialized once, any Global needs to initialised on every activity creation.
 
Upvote 0
Top