Android Question How to do this in b4x

JoséCarlos

Member
Licensed User
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

public class JavaDownloadWebPage2 {
public static void main(String args[]) throws IOException {
download("https://google.com");
}
public static void download(String urlString) throws IOException {
URL url = new URL(urlString);
try(
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
BufferedWriter writer = new BufferedWriter(new FileWriter("page.html"));
) {
String line;
while ((line = reader.readLine()) != null) {
writer.write(line);
}
System.out.println("Page downloaded.");
}
}
}
Java:
 

Alexander Stolte

Expert
Licensed User
Longtime User
?
B4X:
Dim j As HttpJob
j.Initialize("", Me)
j.Download("https://www.google.com")
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
   Log(j.GetString)
End If
j.Release
 
Upvote 1

Cliff McKibbin

Member
Licensed User
Longtime User
I have been using the above code for some time to access txt files and a database on a hosted website.
Erel's original reference was:

At one point, something motivated me to question what would happen if I could not access the internet. I tested using 'airplane' mode and found that the logic falls through and the 'j.download' does not appear to happen.

I have modified the above as shown for the simple case where the app is attempting to read from the internet.

B4X:
Dim j As HttpJob
j.Initialize("", Me)
j.Download("https://www.google.com")
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
   Log(j.GetString)
else
   ToastMessageShow("Unable to access the Internet at this time",False)
   j.release    
   Return    
End If
j.Release
' Continued logic if successful

There are a several other situations in which I have added additional logic in lieu of the 'toastmessage'.

1. Reading 'help' files. My latest help files are txt files on the website. I have added logic to access the help file from the app 'files' folder if the internet fetch fails. The local 'help' entry may or may not be current depending on whether I have put out a new version since the help update.

2. My version of 'push' notifications. I have a routine called from the initial 'Appear' of B4XMainPage in which I look to the website for a message. If it is the same date as the previous message it is ignored, if it is a new date, it is displayed on the bottom of the main menu. If the internet is not accessible, I don't give any message and 'return'.

3. Updating a hosted database. I have a 'Sync' application in which records updated on one device are sent to other users via a hosted website/database. The other users query this website/database periodically to get the latest records.

Sending device
a. Updates the device's own local table.
b. Writes a command line containing the command as well as the data to be updated on the other devices into a local table 'CommandLines'.
c. When the B4XMainPage 'appears' after returning from the update 'pages', a Starter sub is fired that loops through the 'CommandLines' table and sends each command line in turn. If the 'send' loop is successful, it then deletes the records in the 'CommandLines' table. If it fails, the logic 'returns' and the delete is never done. The loop will be attempted next time B4XMainPage 'appears'.
d. In this case the 'CommandLines' table is serving as a buffer. Lines can be added as needed before the loop is successful and the records sent and then deleted.

Receiving device
a similar routine is fired from B4XMainPage to query the remote database. If the Internet connection is not available, the logic 'Returns' and will be tried later. If the read is successful, the records are updated in the local tables and another query is fired to delete the 'hosted' records for this user.

4. If your database is not local and is only 'hosted', you could inform the user that their data is not accessible.
 
Upvote 0

JoséCarlos

Member
Licensed User
That all the Lord explained was all I wanted.
I tried to do something similar, but unfortunately I couldn't.
Couldn't you send an example of the project to see if I can adapt it to mine?
I would be very grateful.
Hugs.
PS. Translated from my Portuguese to English by the translator.
 
Upvote 0

Cliff McKibbin

Member
Licensed User
Longtime User
I had been working on a forum entry for the Sync concepts, but am not done yet.
I'll see if I can finish it up, but it may be a few days.
 
Upvote 0

Cliff McKibbin

Member
Licensed User
Longtime User
I have posted a B4XPages example of a Sync application using a database on a hosted server accessed using PHP.
The application gives an example of sending records to, receiving records from, and deleting records on the remote database.
It comes with both the PHP program and B4XPages test programs.
It is not intended to be a complete Syncing application but does include suggestions for implementing such a program.

 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…