use of com.codename1.io.ConnectionRequest in project CodenameOne by codenameone.
the class RequestBuilder method getAsString.
/**
* Executes the request synchronously
*
* @return Response Object
*/
public Response<String> getAsString() {
ConnectionRequest request = createRequest(false);
CN.addToQueueAndWait(request);
Response res = null;
try {
res = new Response(request.getResponseCode(), new String(request.getResponseData(), "UTF-8"), request.getResponseErrorMessage());
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
}
return res;
}
use of com.codename1.io.ConnectionRequest in project CodenameOne by codenameone.
the class RequestBuilder method getAsBytes.
/**
* Executes the request synchronously
*
* @return Response Object
*/
public Response<byte[]> getAsBytes() {
ConnectionRequest request = createRequest(false);
CN.addToQueueAndWait(request);
Response res = new Response(request.getResponseCode(), request.getResponseData(), request.getResponseErrorMessage());
return res;
}
use of com.codename1.io.ConnectionRequest in project CodenameOne by codenameone.
the class RequestBuilder method getAsStringAsync.
/**
* Executes the request asynchronously and writes the response to the provided
* Callback
* @param callback writes the response to this callback
*/
public void getAsStringAsync(final Callback<Response<String>> callback) {
ConnectionRequest request = createRequest(false);
request.addResponseListener(new ActionListener<NetworkEvent>() {
@Override
public void actionPerformed(NetworkEvent evt) {
Response res = null;
try {
res = new Response(evt.getResponseCode(), new String(evt.getConnectionRequest().getResponseData(), "UTF-8"), evt.getMessage());
callback.onSucess(res);
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
}
}
});
CN.addToQueue(request);
}
use of com.codename1.io.ConnectionRequest 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.io.ConnectionRequest in project CodenameOne by codenameone.
the class NetworkManager method fireProgressEvent.
void fireProgressEvent(ConnectionRequest c, int type, int length, int sentReceived) {
// progressListeners might be made null by a separate thread
EventDispatcher d = progressListeners;
if (d != null) {
NetworkEvent n = new NetworkEvent(c, type);
n.setLength(length);
n.setSentReceived(sentReceived);
d.fireActionEvent(n);
}
}
Aggregations