HttpUtils2

walterf25

Expert
Licensed User
Longtime User
Hello everyone, i'm in need of some help, I'm working on an app that stores the location coordinates to a database when no Internet connection is available or cell phone connection, Upon the device re-gaining either Internet connection or cell phone connection, it will start uploading the saved coordinates to a server.
The part I need help is that I need to figure out how to send each record saved in the database one after the other, so far what I have works if there's only about 15 records saved, but if there's more than that I get an error related to the HttpUtils2 service.

This is the code i have for that part, i know is not the best way to do it which is why I need some help

B4X:
cursor2 = db.ExecQuery("SELECT ID, latitude, longitude, accuracy, speed, time FROM locationinfo")
rows = db.ExecQuerySingleResult("SELECT count(*) FROM locationinfo")

Log("number of records in database :" & rows)
Log("wi_fi " & wifi.GetSettings("wifi_on"))
If rows > 0  AND connection = "connected" OR server.GetMyWifiIP <> wifi_ip Then   'if there's records and internet conection then upload saved data to server

For i = 0 To rows - 1
cursor2.Position = i
latitude2 = cursor2.GetString("latitude")
longitude2 = cursor2.GetString("longitude")
accuracy2 = cursor2.GetString("accuracy")
speed2 = cursor2.GetString("speed")
time2 = cursor2.GetString("time")
coordinates1.Download("http://dev.cyprustech.net/tracking/controller.php?action=coordinate_update&username=" & s_username & "&password=" & s_password & "&i_user_id=" & s_UserID & "&" & "s_lat=" & latitude2 & "&s_long=" & longitude2 & "&s_accuracy=" & accuracy2 & "&s_speed=" & speed2 & "&d_time=" & time2 & "&b_from_saved=1")
db.ExecNonQuery("DELETE FROM locationinfo WHERE ID=" & i)   'delete record when it has been uploaded to server
Log("DELETING FROM locationinfo WHERE ID=" & i)
Log(latitude2 & " " & longitude2 & " " & accuracy2 & " " & speed2 & " " & time2)
DoEvents
Next
index = 0
cursor2.Close
End If

and this is the error i get from the logs
httpjob_getstring2 (B4A line: 120)
tr.Initialize2(File.OpenInput(HttpUtils2Service.TempFolder, taskId), Encoding)
java.io.FileNotFoundException: /mnt/sdcard/Android/data/com.genesis.cyprus/files/32 (No such file or directory)
at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
at java.io.FileInputStream.<init>(FileInputStream.java:80)
at anywheresoftware.b4a.objects.streams.File.OpenInput(File.java:197)
at com.genesis.cyprus.httpjob._getstring2(httpjob.java:301)
at com.genesis.cyprus.httpjob._getstring(httpjob.java:276)
at com.genesis.cyprus.gps_tracker._handlecoordinates1(gps_tracker.java:327)
at com.genesis.cyprus.gps_tracker._jobdone(gps_tracker.java:392)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.keywords.Common$4.run(Common.java:930)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3806)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: java.lang.RuntimeException: java.io.FileNotFoundException: /mnt/sdcard/Android/data/com.genesis.cyprus/files/32 (No such file or directory)
at anywheresoftware.b4a.keywords.Common$4.run(Common.java:933)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3806)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: java.io.FileNotFoundException: /mnt/sdcard/Android/data/com.genesis.cyprus/files/32 (No such file or directory)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:195)
at anywheresoftware.b4a.keywords.Common$4.run(Common.java:930)
... 9 more
Caused by: java.io.FileNotFoundException: /mnt/sdcard/Android/data/com.genesis.cyprus/files/32 (No such file or directory)
at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
at java.io.FileInputStream.<init>(FileInputStream.java:80)
at anywheresoftware.b4a.objects.streams.File.OpenInput(File.java:197)
at com.genesis.cyprus.httpjob._getstring2(httpjob.java:301)
at com.genesis.cyprus.httpjob._getstring(httpjob.java:276)
at com.genesis.cyprus.gps_tracker._handlecoordinates1(gps_tracker.java:327)
at com.genesis.cyprus.gps_tracker._jobdone(gps_tracker.java:392)


at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
... 10 more

i'm guessing i have to wait some time before submitting the next job from this error, hopefully Erel can shed some light on this.

Thanks anyone and everyone who is willing to help.

Thanks,
Walter
 

mc73

Well-Known Member
Licensed User
Longtime User
If there is no true need (probably for compatibility reason) for using a GET, you can send a string as POST, containing all your records, delimited by a special character which you can "explode" at your php script and proceed with the insertions there. If you have many many records, you can even pack them in a dat/txt file and send them for further process at your php script.
If, however, you wish to continue with GETs, a thought is to continue with your loop from inside the jobDone sub, i.e. you send each record by calling a sub, which will be called again when the previous was sent successfully and so on. This can be easily achieved by creating a list containing all your records and firing the sub using the id of the record to process. At least so I suppose, since I haven't used so far, GETs for such operations.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Hi there MC73, thanks for your reply, actually that is exactly what i'm doing right now, inside the "Sub JobDone" i have created a function where it submits the next url link as soon as the previous one is done, i'm testing it right now, hopefully this is what i need, again thanks for your reply, I wish there was an easier way to do this.

cheers,
Walter
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Hi Erel, here's the JobDone code

B4X:
Sub JobDone(job As HttpJob)
   Select job.JobName   'when job is done
      Case "location"   'check which job is done
         Handlecoordinates(job)   'go to handlelogin sub to parse the results
      Case "location1"
         Handlecoordinates1(job)

      Case "location2"
         Handlecoordinates2(job)
   End Select
   job.Release      'delete file
End Sub

Thanks,
Walter
 
Upvote 0
Top