use of com.codename1.builders.util.JSONParser 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.builders.util.JSONParser 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.builders.util.JSONParser in project CodenameOne by codenameone.
the class ConnectionRequest method fetchJSONAsync.
/**
* Fetches JSON asynchronously.
* @param url The URL to fetch.
* @return AsyncResource that will resolve with either an exception or the parsed JSON data.
* @since 7.0
*/
public static AsyncResource<Map<String, Object>> fetchJSONAsync(String url) {
final AsyncResource<Map<String, Object>> out = new AsyncResource<Map<String, Object>>();
final ConnectionRequest cr = new ConnectionRequest();
cr.setFailSilently(true);
cr.setPost(false);
cr.setUrl(url);
cr.addResponseListener(new ActionListener<NetworkEvent>() {
@Override
public void actionPerformed(NetworkEvent evt) {
if (out.isDone()) {
return;
}
if (cr.getResponseData() == null) {
if (cr.failureException != null) {
out.error(new IOException(cr.failureException.toString()));
return;
} else {
out.error(new IOException("Server returned error code: " + cr.failureErrorCode));
return;
}
}
JSONParser jp = new JSONParser();
Map<String, Object> result = null;
try {
result = jp.parseJSON(new InputStreamReader(new ByteArrayInputStream(cr.getResponseData()), "UTF-8"));
} catch (IOException ex) {
out.error(ex);
return;
}
out.complete(result);
}
});
NetworkManager.getInstance().addToQueue(cr);
return out;
}
use of com.codename1.builders.util.JSONParser in project CodenameOne by codenameone.
the class VServAds method createAdRequest.
/**
* {@inheritDoc}
*/
protected ConnectionRequest createAdRequest() {
ConnectionRequest con = new ConnectionRequest() {
protected void handleErrorResponseCode(int code, String message) {
failed = true;
}
protected void handleException(Exception err) {
failed = true;
Log.e(err);
}
private String getString(Hashtable h, String n) {
Object v = h.get(n);
if (v == null) {
return null;
}
if (v instanceof Vector) {
return (String) ((Vector) v).elementAt(0);
}
return (String) v;
}
protected void readResponse(InputStream input) throws IOException {
JSONParser parser = new JSONParser();
Hashtable h = parser.parse(new InputStreamReader(input, "UTF-8"));
if (h.size() == 0) {
return;
}
backgroundColor = Integer.parseInt((String) ((Hashtable) ((Vector) h.get("style")).elementAt(0)).get("background-color"), 16);
Hashtable actionHash = ((Hashtable) ((Vector) h.get("action")).elementAt(0));
actionNotify = getString(actionHash, "notify");
if (actionNotify == null) {
actionNotify = getString(actionHash, "notify-once");
}
destination = (String) actionHash.get("data");
Hashtable renderHash = ((Hashtable) ((Vector) h.get("render")).elementAt(0));
contentType = (String) renderHash.get("type");
renderNotify = getString(renderHash, "notify");
if (renderNotify == null) {
renderNotify = getString(renderHash, "notify-once");
}
imageURL = (String) renderHash.get("data");
}
};
con.setUrl(URL);
con.setPost(false);
con.addArgument("zoneid", getZoneId());
con.addArgument("ua", Display.getInstance().getProperty("User-Agent", ""));
con.addArgument("app", "1");
con.addArgument("aid", Display.getInstance().getProperty("androidId", ""));
con.addArgument("uuid", Display.getInstance().getProperty("uuid", ""));
con.addArgument("im", Display.getInstance().getProperty("imei", ""));
con.addArgument("sw", "" + Display.getInstance().getDisplayWidth());
con.addArgument("sh", "" + Display.getInstance().getDisplayHeight());
con.addArgument("mn", Display.getInstance().getProperty("AppName", ""));
con.addArgument("vs3", "1");
con.addArgument("partnerid", "1");
con.addArgument("zc", "" + category);
if (countryCode != null) {
con.addArgument("cc", countryCode);
}
if (networkCode != null) {
con.addArgument("nc", networkCode);
}
if (locale != null) {
con.addArgument("lc", locale);
}
return con;
}
use of com.codename1.builders.util.JSONParser in project CodenameOne by codenameone.
the class PropertyIndex method loadJSON.
/**
* Loads JSON for the object from the given input stream
* @param stream the input stream containing the JSON file
*/
public void loadJSON(InputStream stream) throws IOException {
JSONParser jp = new JSONParser();
JSONParser.setUseBoolean(true);
JSONParser.setUseLongs(true);
populateFromMap(jp.parseJSON(new InputStreamReader(stream, "UTF-8")), parent.getClass());
}
Aggregations