use of com.codename1.io.NetworkEvent in project CodenameOne by codenameone.
the class ConnectionRequest method downloadImage.
private void downloadImage(final SuccessCallback<Image> onSuccess, final FailureCallback<Image> onFail, boolean useCache) {
setReadResponseForErrors(false);
if (useCache) {
Display.getInstance().scheduleBackgroundTask(new Runnable() {
public void run() {
if (getDestinationFile() != null) {
String file = getDestinationFile();
FileSystemStorage fs = FileSystemStorage.getInstance();
if (fs.exists(file)) {
try {
EncodedImage img = EncodedImage.create(fs.openInputStream(file), (int) fs.getLength(file));
if (img == null) {
throw new IOException("Failed to load image at " + file);
}
CallbackDispatcher.dispatchSuccess(onSuccess, img);
} catch (Exception ex) {
CallbackDispatcher.dispatchError(onFail, ex);
}
} else {
downloadImage(onSuccess, onFail, false);
}
} else if (getDestinationStorage() != null) {
String file = getDestinationStorage();
Storage fs = Storage.getInstance();
if (fs.exists(file)) {
try {
EncodedImage img = EncodedImage.create(fs.createInputStream(file), fs.entrySize(file));
if (img == null) {
throw new IOException("Failed to load image at " + file);
}
CallbackDispatcher.dispatchSuccess(onSuccess, img);
} catch (Exception ex) {
CallbackDispatcher.dispatchError(onFail, ex);
}
} else {
downloadImage(onSuccess, onFail, false);
}
}
}
});
} else {
final ActionListener onDownload = new ActionListener<NetworkEvent>() {
public void actionPerformed(NetworkEvent nevt) {
int rc = nevt.getResponseCode();
if (rc == 200 || rc == 201) {
downloadImage(onSuccess, onFail, true);
} else {
if (nevt.getError() == null) {
nevt.setError(new IOException("Failed to get image: Code was " + nevt.getResponseCode()));
}
CallbackDispatcher.dispatchError(onFail, nevt.getError());
}
removeResponseListener(this);
}
};
addResponseListener(onDownload);
NetworkManager.getInstance().addToQueue(this);
}
}
use of com.codename1.io.NetworkEvent in project CodenameOne by codenameone.
the class ConnectionRequest method addResponseCodeListener.
/**
* Adds a listener that would be notified on the CodenameOne thread of a response code that
* is not a 200 (OK) or 301/2 (redirect) response code.
*
* @param a listener
*/
public void addResponseCodeListener(ActionListener<NetworkEvent> a) {
if (responseCodeListeners == null) {
responseCodeListeners = new EventDispatcher();
responseCodeListeners.setBlocking(false);
}
responseCodeListeners.addListener(a);
}
use of com.codename1.io.NetworkEvent in project CodenameOne by codenameone.
the class SliderBridge method bindProgress.
/**
* Allows binding progress to an arbitrary slider
*
* @param sources the source connection request (null for all network activity)
* @param s the slider
*/
public static void bindProgress(final ConnectionRequest[] sources, final Slider s) {
Vector v = null;
int portions = 100;
if (sources != null) {
v = new Vector();
int slen = sources.length;
for (int iter = 0; iter < slen; iter++) {
v.addElement(sources[iter]);
}
portions = portions / slen;
}
final Vector sourceVec = v;
final int portionPerSource = portions;
NetworkManager.getInstance().addProgressListener(new ActionListener() {
private float currentLength;
private int soFar;
/**
* {@inheritDoc}
*/
public void actionPerformed(ActionEvent evt) {
if (sources != null) {
if (!sourceVec.contains(evt.getSource())) {
return;
}
}
NetworkEvent e = (NetworkEvent) evt;
switch(e.getProgressType()) {
case NetworkEvent.PROGRESS_TYPE_COMPLETED:
s.setInfinite(false);
// s.setProgress(s.getMaxValue());
soFar += portionPerSource;
s.setProgress(soFar);
if (sources != null) {
NetworkManager.getInstance().removeProgressListener(this);
}
break;
case NetworkEvent.PROGRESS_TYPE_INITIALIZING:
s.setInfinite(true);
break;
case NetworkEvent.PROGRESS_TYPE_INPUT:
case NetworkEvent.PROGRESS_TYPE_OUTPUT:
if (e.getLength() > 0) {
currentLength = e.getLength();
// s.setMaxValue(1000);
s.setInfinite(false);
float sentReceived = e.getSentReceived();
sentReceived = sentReceived / currentLength * portionPerSource;
s.setProgress((int) sentReceived + soFar);
// s.setProgress(e.getSentReceived());
// s.setMaxValue(e.getLength());
} else {
s.setInfinite(true);
}
break;
}
}
});
}
use of com.codename1.io.NetworkEvent in project CodenameOne by codenameone.
the class RequestBuilder method getAsStringAsync.
/**
* Executes the request asynchronously and writes the response to the provided
* Callback
* @param callback writes the response to this callback
*/
public void getAsStringAsync(final Callback<Response<String>> callback) {
ConnectionRequest request = createRequest(false);
request.addResponseListener(new ActionListener<NetworkEvent>() {
@Override
public void actionPerformed(NetworkEvent evt) {
Response res = null;
try {
res = new Response(evt.getResponseCode(), new String(evt.getConnectionRequest().getResponseData(), "UTF-8"), evt.getMessage());
callback.onSucess(res);
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
}
}
});
CN.addToQueue(request);
}
use of com.codename1.io.NetworkEvent in project CodenameOne by codenameone.
the class CachedDataService method readResponse.
/**
* {@inheritDoc}
*/
protected void readResponse(InputStream input) throws IOException {
data.setData(Util.readInputStream(input));
fireResponseListener(new NetworkEvent(this, data));
data.setFetching(false);
}
Aggregations