Search in sources :

Example 6 with JSONParser

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();
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) InputStreamReader(java.io.InputStreamReader) InfiniteProgress(com.codename1.components.InfiniteProgress) ByteArrayInputStream(java.io.ByteArrayInputStream) Form(com.codename1.ui.Form) Dialog(com.codename1.ui.Dialog) JSONParser(com.codename1.io.JSONParser) IOException(java.io.IOException) Map(java.util.Map)

Example 7 with JSONParser

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();
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) InputStreamReader(java.io.InputStreamReader) InfiniteProgress(com.codename1.components.InfiniteProgress) ByteArrayInputStream(java.io.ByteArrayInputStream) Form(com.codename1.ui.Form) Dialog(com.codename1.ui.Dialog) JSONParser(com.codename1.io.JSONParser) IOException(java.io.IOException) Map(java.util.Map)

Example 8 with JSONParser

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;
}
Also used : InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) AsyncResource(com.codename1.util.AsyncResource) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 9 with JSONParser

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;
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) InputStreamReader(java.io.InputStreamReader) Hashtable(java.util.Hashtable) InputStream(java.io.InputStream) JSONParser(com.codename1.io.JSONParser) Vector(java.util.Vector) IOException(java.io.IOException)

Example 10 with JSONParser

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());
}
Also used : InputStreamReader(java.io.InputStreamReader) JSONParser(com.codename1.io.JSONParser)

Aggregations

JSONParser (com.codename1.io.JSONParser)13 InputStreamReader (java.io.InputStreamReader)12 Map (java.util.Map)11 IOException (java.io.IOException)9 ConnectionRequest (com.codename1.io.ConnectionRequest)6 InputStream (java.io.InputStream)5 AsyncResource (com.codename1.util.AsyncResource)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 HashMap (java.util.HashMap)4 ArrayList (java.util.ArrayList)3 Hashtable (java.util.Hashtable)3 LinkedHashMap (java.util.LinkedHashMap)3 InfiniteProgress (com.codename1.components.InfiniteProgress)2 Dialog (com.codename1.ui.Dialog)2 Form (com.codename1.ui.Form)2 StringReader (com.codename1.util.regex.StringReader)2 List (java.util.List)2 Intent (android.content.Intent)1 JSONParser (com.codename1.builders.util.JSONParser)1 IntentResultListener (com.codename1.impl.android.IntentResultListener)1