use of com.codename1.io.NetworkEvent in project CodenameOne by codenameone.
the class TwitterRESTService method readResponse.
/**
* {@inheritDoc}
*/
protected void readResponse(InputStream input) throws IOException {
InputStreamReader i = new InputStreamReader(input, "UTF-8");
parseTree = new JSONParser().parse(i);
fireResponseListener(new NetworkEvent(this, parseTree));
}
use of com.codename1.io.NetworkEvent in project CodenameOne by codenameone.
the class NetworkManager method fireProgressEvent.
void fireProgressEvent(ConnectionRequest c, int type, int length, int sentReceived) {
// progressListeners might be made null by a separate thread
EventDispatcher d = progressListeners;
if (d != null) {
NetworkEvent n = new NetworkEvent(c, type);
n.setLength(length);
n.setSentReceived(sentReceived);
d.fireActionEvent(n);
}
}
use of com.codename1.io.NetworkEvent in project CodenameOne by codenameone.
the class NetworkManager method handleException.
private boolean handleException(ConnectionRequest r, Exception o) {
if (errorListeners != null) {
ActionEvent ev = new NetworkEvent(r, o);
errorListeners.fireActionEvent(ev);
return ev.isConsumed();
}
return false;
}
use of com.codename1.io.NetworkEvent in project CodenameOne by codenameone.
the class NetworkManager method addErrorListener.
/**
* Adds a generic listener to a network error that is invoked before the exception is propagated.
* Notice that this doesn't apply to server error codes!
* Consume the event in order to prevent it from propagating further.
*
* @param e callback will be invoked with the Exception as the source object
*/
public void addErrorListener(ActionListener<NetworkEvent> e) {
if (errorListeners == null) {
errorListeners = new EventDispatcher();
errorListeners.setBlocking(true);
}
errorListeners.addListener(e);
}
use of com.codename1.io.NetworkEvent in project CodenameOne by codenameone.
the class FacebookShare method share.
/**
* {@inheritDoc}
*/
public void share(String text, final String image, final String mime) {
final ShareForm[] f = new ShareForm[1];
if (image == null) {
f[0] = new ShareForm(getOriginal(), "Post on My Wall", null, text, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try {
InfiniteProgress inf = new InfiniteProgress();
final Dialog progress = inf.showInifiniteBlocking();
FaceBookAccess.getInstance().addResponseCodeListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
NetworkEvent ne = (NetworkEvent) evt;
int code = ne.getResponseCode();
FaceBookAccess.getInstance().removeResponseCodeListener(this);
progress.dispose();
Dialog.show("Failed to Share", "for some reason sharing has failed, try again later.", "Ok", null);
finish();
}
});
FaceBookAccess.getInstance().postOnWall("me", f[0].getMessage(), new ActionListener() {
public void actionPerformed(ActionEvent evt) {
progress.dispose();
finish();
}
});
} catch (IOException ex) {
Log.e(ex);
System.out.println("failed to share " + ex.getMessage());
}
}
});
f[0].show();
} else {
f[0] = new ShareForm(getOriginal(), "Post on My Wall", null, text, image, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
InfiniteProgress inf = new InfiniteProgress();
final Dialog progress = inf.showInifiniteBlocking();
FaceBookAccess.getInstance().addResponseCodeListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
NetworkEvent ne = (NetworkEvent) evt;
int code = ne.getResponseCode();
FaceBookAccess.getInstance().removeResponseCodeListener(this);
progress.dispose();
Dialog.show("Failed to Share", "for some reason sharing has failed, try again later.", "Ok", null);
finish();
}
});
MultipartRequest req = new MultipartRequest();
req.addResponseListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
progress.dispose();
finish();
}
});
final String endpoint = "https://graph.facebook.com/me/photos?access_token=" + token;
req.setUrl(endpoint);
req.addArgumentNoEncoding("message", f[0].getMessage());
InputStream is = null;
try {
is = FileSystemStorage.getInstance().openInputStream(image);
req.addData("source", is, FileSystemStorage.getInstance().getLength(image), mime);
NetworkManager.getInstance().addToQueue(req);
} catch (IOException ioe) {
Log.e(ioe);
}
}
});
f[0].show();
}
}
Aggregations