Search in sources :

Example 36 with ConnectionRequest

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;
}
Also used : GZConnectionRequest(com.codename1.io.gzip.GZConnectionRequest) ConnectionRequest(com.codename1.io.ConnectionRequest) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 37 with ConnectionRequest

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;
}
Also used : GZConnectionRequest(com.codename1.io.gzip.GZConnectionRequest) ConnectionRequest(com.codename1.io.ConnectionRequest)

Example 38 with ConnectionRequest

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);
}
Also used : GZConnectionRequest(com.codename1.io.gzip.GZConnectionRequest) ConnectionRequest(com.codename1.io.ConnectionRequest) NetworkEvent(com.codename1.io.NetworkEvent) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 39 with ConnectionRequest

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;
}
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 40 with ConnectionRequest

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);
    }
}
Also used : EventDispatcher(com.codename1.ui.util.EventDispatcher)

Aggregations

ConnectionRequest (com.codename1.io.ConnectionRequest)41 IOException (java.io.IOException)11 GZConnectionRequest (com.codename1.io.gzip.GZConnectionRequest)7 InputStream (java.io.InputStream)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 Map (java.util.Map)6 JSONParser (com.codename1.io.JSONParser)5 ActionEvent (com.codename1.ui.events.ActionEvent)5 InputStreamReader (java.io.InputStreamReader)5 InfiniteProgress (com.codename1.components.InfiniteProgress)4 NetworkEvent (com.codename1.io.NetworkEvent)4 Dialog (com.codename1.ui.Dialog)4 Form (com.codename1.ui.Form)4 ActionListener (com.codename1.ui.events.ActionListener)3 DataInputStream (java.io.DataInputStream)3 Hashtable (java.util.Hashtable)3 Component (com.codename1.ui.Component)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 HashMap (java.util.HashMap)2 Vector (java.util.Vector)2