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;
}
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));
}
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;
}
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;
}
Aggregations