-
-
Notifications
You must be signed in to change notification settings - Fork 695
Advanced topics
For security reasons, the library doesn't accept self-signed certificates by default when using HTTPS connections, but you can enable them by calling:
AllCertificatesAndHostsTruster.apply();
before starting the upload service.
Sometimes you need to login into your server before doing uploads, and your implementation may have been done with sessions, so you need to pass cookies in the request. How to do that?
If you use HttpUrlConnection
to perform the login into your server, add the following two lines into your Application
subclass, before any network request is done:
CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
and cookie management will be done automatically for you!
If you use OkHttp
, setup a persistent cookie store, for example this one by Fran Montiel before doing any request. Add this method somewhere in your code:
public String getSessionCookie(PersistentCookieStore persistentCookieStore, String sessionCookieName) {
if (persistentCookieStore != null && sessionCookieName != null) {
for (final HttpCookie cookie : persistentCookieStore.getCookies()) {
if (cookie.getName().equalsIgnoreCase(sessionCookieName)) {
return cookie.getValue();
}
}
}
return null;
}
Then, when you build your upload request:
String sessionCookieValue = getSessionCookie(persistentCookieStore, sessionCookieName);
If sessionCookieValue
is not null, add this statement in the upload request, before startUpload()
:
.setHeader("Cookie", sessionCookieName + "=" + sessionCookieValue);
Where sessionCookieName
is a string containing the name of your session cookie, defined somewhere in your code.
Android Upload Service tries its best to upload files. You can in fact set the maximum number of automatic upload retries before notifying an error, by calling the setMaxRetries
on the upload request. Since network on mobile devices is unreliable and it may also happen that your server is not reachable when you try to upload something, it may happen that your files are not uploaded even after 100 automatic retries (which is a bad practice btw 😄). So, what I suggest you to do is to keep a reference to the uploadID and the upload request object somewhere in your code, so you can trigger upload again by calling the startUpload
method on the request object when certain conditions (e.g. server is reachable again) are met. How to properly implement that behaviour for your specific scenario is up to you. If you can't figure it out how to do it and you need some further help, you're welcome, but that's called consulting 😃 .
If you want to start uploads or retry them based on the remote server's reachability status, Android Host Monitor may be useful to you in combination with this library.