use of com.codename1.io.ConnectionRequest in project CodenameOne by codenameone.
the class SignIn method showGoogleUser.
private void showGoogleUser(String token) {
ConnectionRequest req = new ConnectionRequest();
req.addRequestHeader("Authorization", "Bearer " + token);
req.setUrl("https://www.googleapis.com/plus/v1/people/me");
req.setPost(false);
InfiniteProgress ip = new InfiniteProgress();
Dialog d = ip.showInifiniteBlocking();
NetworkManager.getInstance().addToQueueAndWait(req);
d.dispose();
byte[] data = req.getResponseData();
JSONParser parser = new JSONParser();
Map map = null;
try {
map = parser.parseJSON(new InputStreamReader(new ByteArrayInputStream(data), "UTF-8"));
} catch (IOException ex) {
ex.printStackTrace();
}
String name = (String) map.get("displayName");
Map im = (Map) map.get("image");
String url = (String) im.get("url");
Form userForm = new UserForm(name, (EncodedImage) theme.getImage("user.png"), url);
userForm.show();
}
use of com.codename1.io.ConnectionRequest in project CodenameOne by codenameone.
the class SignIn method showFacebookUser.
private void showFacebookUser(String token) {
ConnectionRequest req = new ConnectionRequest();
req.setPost(false);
req.setUrl("https://graph.facebook.com/v2.3/me");
req.addArgumentNoEncoding("access_token", token);
InfiniteProgress ip = new InfiniteProgress();
Dialog d = ip.showInifiniteBlocking();
NetworkManager.getInstance().addToQueueAndWait(req);
byte[] data = req.getResponseData();
JSONParser parser = new JSONParser();
Map map = null;
try {
map = parser.parseJSON(new InputStreamReader(new ByteArrayInputStream(data), "UTF-8"));
} catch (IOException ex) {
ex.printStackTrace();
}
String name = (String) map.get("name");
d.dispose();
Form userForm = new UserForm(name, (EncodedImage) theme.getImage("user.png"), "https://graph.facebook.com/v2.3/me/picture?access_token=" + token);
userForm.show();
}
use of com.codename1.io.ConnectionRequest in project CodenameOne by codenameone.
the class CodenameOneImplementation method downloadImageToFileSystem.
/**
* Downloads an image to file system. This will *not* first check to see if the file exists already.
* It will download and overwrite any existing image at the provided location.
*
* <p>Some platforms may override this method to use platform-level caching. E.g. Javascript will use
* the browser cache for downloading the image.</p>
*
* @param url The URL of the image to download.
* @param fileName The storage key to be used to store the image.
* @param onSuccess Callback on success. Will be executed on EDT.
* @param onFail Callback on failure. Will be executed on EDT.
*/
public void downloadImageToFileSystem(String url, String fileName, SuccessCallback<Image> onSuccess, FailureCallback<Image> onFail) {
ConnectionRequest cr = new ConnectionRequest();
cr.setPost(false);
cr.setFailSilently(true);
cr.setReadResponseForErrors(false);
cr.setDuplicateSupported(true);
cr.setUrl(url);
cr.downloadImageToFileSystem(fileName, onSuccess, onFail);
}
use of com.codename1.io.ConnectionRequest in project CodenameOne by codenameone.
the class CodenameOneImplementation method downloadImageToStorage.
/**
* Downloads an image to storage. This will *not* first check to see if the image is located in storage
* already. It will download and overwrite any existing image at the provided location.
*
* <p>Some platforms may override this method to use platform-level caching. E.g. Javascript will use
* the browser cache for downloading the image.</p>
*
* @param url The URL of the image to download.
* @param fileName The storage key to be used to store the image.
* @param onSuccess Callback on success. Will be executed on EDT.
* @param onFail Callback on failure. Will be executed on EDT.
*/
public void downloadImageToStorage(String url, String fileName, SuccessCallback<Image> onSuccess, FailureCallback<Image> onFail) {
ConnectionRequest cr = new ConnectionRequest();
cr.setPost(false);
cr.setFailSilently(true);
cr.setReadResponseForErrors(false);
cr.setDuplicateSupported(true);
cr.setUrl(url);
cr.downloadImageToStorage(fileName, onSuccess, onFail);
}
use of com.codename1.io.ConnectionRequest in project CodenameOne by codenameone.
the class CodenameOneImplementation method registerPollingFallback.
/**
* Registers a polling thread to simulate push notification
*/
protected static void registerPollingFallback() {
if (pollingThreadRunning || callback == null) {
return;
}
pollingThreadRunning = true;
final long pushId = Preferences.get("push_id", (long) -1);
if (pushId > -1) {
new CodenameOneThread(new Runnable() {
public void run() {
String lastReq = Preferences.get("last_push_req", "0");
while (pollingThreadRunning) {
try {
ConnectionRequest cr = new ConnectionRequest();
cr.setUrl(Display.getInstance().getProperty("cloudServerURL", "https://codename-one.appspot.com/") + "pollManualPush");
cr.setPost(false);
cr.setFailSilently(true);
cr.addArgument("i", "" + pushId);
cr.addArgument("last", lastReq);
NetworkManager.getInstance().addToQueueAndWait(cr);
if (cr.getResponseCode() != 200) {
callback.pushRegistrationError("Server registration error", 1);
} else {
DataInputStream di = new DataInputStream(new ByteArrayInputStream(cr.getResponseData()));
if (di.readBoolean()) {
byte type = di.readByte();
String message = di.readUTF();
lastReq = "" + di.readLong();
Preferences.set("last_push_req", lastReq);
callback.push(message);
}
}
} catch (IOException ex) {
Log.e(ex);
}
try {
synchronized (callback) {
callback.wait(pollingMillis);
}
} catch (Throwable t) {
Log.e(t);
}
}
}
}, "Polling Thread").start();
}
}
Aggregations