Android Question [Solved]Issue where code exits loop in unexpected manner.

DataMiser

Member
Licensed User
So I have the code below in an app I am working on. The code is called after having made a connection to a TCP server. The intention is for it to read a file and send one line at a time to the server, wait for a response then increment one of two variables based on the response then continue to send the next line until it reaches the end of file.

The code is connecting, reads the first line from the file, sends it and gets the proper response. Stepping through the code in debug mode I can see that it executes the line intGood=intGood+1 but then it jumps to the IP.Close statement near the bottom.

Any idea why this would be happening?

B4X:
Sub UploadData() As Boolean
If File.Exists(File.DirDefaultExternal,"Data.txt") Then
   Dim sReader As TextReader,sRecord As String
   Dim IPReader As TextReader,IPWriter As TextWriter
   Dim IntGood As Int, intBad As Int
   Dim fTotal As Long,fProcessed As Long
   fTotal=File.Size(File.DirDefaultExternal,"Data.txt")
   IPReader.Initialize(IP.InputStream)
   IPWriter.Initialize(IP.OutputStream)
   ProgressBar1.Progress=0
   sReader.Initialize(File.OpenInput(File.DirDefaultExternal,"Data.txt"))
   Do While sReader.Ready
     sRecord=sReader.readline 
     If sRecord.Length>1 Then
          fProcessed=fProcessed+sRecord.Length
          ProgressBar1.Progress=Round(fProcessed/fTotal*100)
          IPWriter.WriteLine(sRecord)
          IPWriter.Flush
          sRecord=IPReader.readline
          If sRecord.StartsWith("OK") Then
              IntGood=IntGood+1  ' Executes this line then jumps to IP.Close
          Else
              intBad=intBad+1
          End If
     Else
          Exit
     End If
  Loop
  sReader.close
End If
IP.close   ' Jumps here after the OK response??
Return True
End Sub
 

DataMiser

Member
Licensed User
Never mind. The problem turned out to be the file I was reading. I thought there was 4 lines in the file but as it turns out one of the functions had append set to false and overwrote the file so it contained only one record. The debugger jumping to the bottom was causing the confusion and preventing me from noticing that it was actually working.
 
Upvote 0
Top