Last active
June 2, 2020 22:53
-
-
Save jsfan3/554590a12c3102a3d77e17533e7eca98 to your computer and use it in GitHub Desktop.
Codename One Util.downloadUrlSafely example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.codename1.components.SpanLabel; | |
import com.codename1.components.ToastBar; | |
import static com.codename1.ui.CN.*; | |
import com.codename1.ui.Display; | |
import com.codename1.ui.Form; | |
import com.codename1.ui.Dialog; | |
import com.codename1.ui.Label; | |
import com.codename1.ui.plaf.UIManager; | |
import com.codename1.ui.util.Resources; | |
import com.codename1.io.Log; | |
import com.codename1.ui.Toolbar; | |
import java.io.IOException; | |
import com.codename1.ui.layouts.BoxLayout; | |
import com.codename1.io.NetworkEvent; | |
import com.codename1.io.Storage; | |
import com.codename1.io.Util; | |
import java.util.Timer; | |
import java.util.TimerTask; | |
/** | |
* This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose | |
* of building native mobile applications using Java. | |
*/ | |
public class MyApplication { | |
private Form current; | |
private Resources theme; | |
public void init(Object context) { | |
// use two network threads instead of one | |
updateNetworkThreadCount(2); | |
theme = UIManager.initFirstTheme("/theme"); | |
// Enable Toolbar on all Forms by default | |
Toolbar.setGlobalToolbar(true); | |
// Pro only feature | |
Log.bindCrashProtection(true); | |
// Manage both network errors (connectivity issues) and server errors (codes different from 2xx) | |
addNetworkAndServerErrorListener(); | |
} | |
public void start() { | |
if(current != null){ | |
current.show(); | |
return; | |
} | |
String url = "https://www.informatica-libera.net/video/AVO_Cariati_Pasqua_2020.mp4"; // 38 MB | |
Form form = new Form("Test Download 38MB", BoxLayout.y()); | |
Label infoLabel = new Label("Starting download..."); | |
form.add(infoLabel); | |
try { | |
Util.downloadUrlSafely(url, "myHeavyVideo.mp4", (percentage) -> { | |
// percentage callback | |
infoLabel.setText("Downloaded: " + percentage + "%"); | |
infoLabel.repaint(); | |
}, (filename) -> { | |
// file saved callback | |
infoLabel.setText("Downloaded completed"); | |
int fileSizeMB = Storage.getInstance().entrySize(filename) / 1048576; | |
form.add("Checking files size: " + fileSizeMB + " MB"); | |
form.revalidate(); | |
}); | |
} catch (IOException ex) { | |
Log.p("Error in downloading: " + url); | |
Log.e(ex); | |
form.add(new SpanLabel("Error in downloading:\n" + url)); | |
form.revalidate(); | |
} | |
form.show(); | |
} | |
public void stop() { | |
current = getCurrentForm(); | |
if(current instanceof Dialog) { | |
((Dialog)current).dispose(); | |
current = getCurrentForm(); | |
} | |
} | |
public void destroy() { | |
} | |
private void addNetworkAndServerErrorListener() { | |
// The following way to manage network errors is discussed here: | |
// https://stackoverflow.com/questions/61993127/distinguish-between-server-side-errors-and-connection-problems | |
addNetworkErrorListener(err -> { | |
// prevents the event from propagating | |
err.consume(); | |
if (err.getError() != null) { | |
// this is the case of a network error, | |
// like: java.io.IOException: Unreachable | |
Log.p("Error connectiong to: " + err.getConnectionRequest().getUrl(), Log.ERROR); | |
// maybe there are connectivity issues, let's try again | |
ToastBar.showInfoMessage("Reconnect..."); | |
Timer timer = new Timer(); | |
timer.schedule(new TimerTask() { | |
@Override | |
public void run() { | |
err.getConnectionRequest().retry(); | |
} | |
}, 2000); | |
} else { | |
// this is the case of a server error | |
// logs the error | |
String errorLog = "REST ERROR\nURL:" + err.getConnectionRequest().getUrl() | |
+ "\nMethod: " + err.getConnectionRequest().getHttpMethod() | |
+ "\nResponse code: " + err.getConnectionRequest().getResponseCode(); | |
if (err.getConnectionRequest().getRequestBody() != null) { | |
errorLog += "\nRequest body: " + err.getConnectionRequest().getRequestBody(); | |
} | |
if (err.getConnectionRequest().getResponseData() != null) { | |
errorLog += "\nResponse message: " + new String(err.getConnectionRequest().getResponseData()); | |
} | |
if (err.getConnectionRequest().getResponseErrorMessage() != null) { | |
errorLog += "\nResponse error message: " + err.getConnectionRequest().getResponseErrorMessage(); | |
} | |
Log.p(errorLog, Log.ERROR); | |
Log.sendLogAsync(); | |
ToastBar.showErrorMessage("Server Error", 10000); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment