Search in sources :

Example 16 with JSONParser

use of com.codename1.builders.util.JSONParser in project CodenameOne by codenameone.

the class TwitterRESTService method initToken.

/**
 * Logs in to twitter as an application
 *
 * @param consumerKey the key to login with
 * @param consumerSecret the secret to to login with
 * @return the authorization token
 */
public static String initToken(String consumerKey, String consumerSecret) {
    ConnectionRequest auth = new ConnectionRequest() {

        protected void readResponse(InputStream input) throws IOException {
            JSONParser p = new JSONParser();
            Hashtable h = p.parse(new InputStreamReader(input));
            authToken = (String) h.get("access_token");
            if (authToken == null) {
                return;
            }
        }
    };
    auth.setPost(true);
    auth.setUrl("https://api.twitter.com/oauth2/token");
    // YOU MUST CHANGE THIS IF YOU BUILD YOUR OWN APP
    String encoded = Base64.encodeNoNewline((consumerKey + ":" + consumerSecret).getBytes());
    auth.addRequestHeader("Authorization", "Basic " + encoded);
    auth.setContentType("application/x-www-form-urlencoded;charset=UTF-8");
    auth.addArgument("grant_type", "client_credentials");
    NetworkManager.getInstance().addToQueueAndWait(auth);
    return authToken;
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Hashtable(java.util.Hashtable) JSONParser(com.codename1.io.JSONParser)

Example 17 with JSONParser

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

Example 18 with JSONParser

use of com.codename1.builders.util.JSONParser in project CodenameOne by codenameone.

the class AutocompleteOverrideFilterSample method searchLocations.

String[] searchLocations(String text) {
    try {
        if (text.length() > 0) {
            if (currRequest != null) {
            // currRequest.kill();
            }
            ConnectionRequest r = new ConnectionRequest();
            currRequest = r;
            r.setPost(false);
            r.setUrl("https://maps.googleapis.com/maps/api/place/autocomplete/json");
            r.addArgument("key", apiKey.getText());
            r.addArgument("input", text);
            NetworkManager.getInstance().addToQueueAndWait(r);
            Map<String, Object> result = new JSONParser().parseJSON(new InputStreamReader(new ByteArrayInputStream(r.getResponseData()), "UTF-8"));
            String[] res = Result.fromContent(result).getAsStringArray("//description");
            return res;
        }
    } catch (Exception err) {
        Log.e(err);
    }
    return null;
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) JSONParser(com.codename1.io.JSONParser) IOException(java.io.IOException)

Example 19 with JSONParser

use of com.codename1.builders.util.JSONParser in project CodeRAD by shannah.

the class ParsingService method parseJSON.

public AsyncResource<Map> parseJSON(String content, JSONParser parser) {
    AsyncResource<Map> out = new AsyncResource<Map>();
    start();
    thread.run(() -> {
        try {
            Map m = parser.parseJSON(new StringReader(content));
            out.complete(m);
        } catch (Throwable ex) {
            out.error(ex);
        }
    });
    return out;
}
Also used : StringReader(java.io.StringReader) AsyncResource(com.codename1.util.AsyncResource) Map(java.util.Map)

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