Java Code Examples for com.codename1.io.ConnectionRequest#addArgument()

The following examples show how to use com.codename1.io.ConnectionRequest#addArgument() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ServerAccess.java    From codenameone-demos with GNU General Public License v2.0 7 votes vote down vote up
public static List getEntriesFromFlickrService(String tag) {
    
    try {
        ConnectionRequest req = new ConnectionRequest();
        req.setUrl("http://api.flickr.com/services/feeds/photos_public.gne");
        req.setPost(false);
        req.addArgument("tagmode", "any");
        req.addArgument("tags", tag);
        req.addArgument("format", "json");
        
        NetworkManager.getInstance().addToQueueAndWait(req);
        byte[] data = req.getResponseData();
        if (data == null) {
            throw new IOException("Network Err");
        }
        JSONParser parser = new JSONParser();
        Map response = parser.parseJSON(new InputStreamReader(new ByteArrayInputStream(data), "UTF-8"));
        System.out.println("res" + response);
        List items = (List)response.get("items");
        return items;
    } catch (Exception e) {
    }
    return null;
}
 
Example 2
Source File: AnalyticsService.java    From CodenameOne with GNU General Public License v2.0 6 votes vote down vote up
private static ConnectionRequest GetGARequest() {
    ConnectionRequest req = new ConnectionRequest();
    req.setUrl("https://www.google-analytics.com/collect");
    req.setPost(true);
    req.setFailSilently(true);
    req.addArgument("v", "1");
    req.addArgument("tid", instance.agent);
    if (timeout > 0) {
        req.setTimeout(timeout);
    }
    if (readTimeout > 0) {
        req.setReadTimeout(readTimeout);
    }
    long uniqueId = Log.getUniqueDeviceId();
    req.addArgument("cid", String.valueOf(uniqueId));
    return req;
}
 
Example 3
Source File: CloudStorage.java    From CodenameOne with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Deletes a file from the cloud storage
 * 
 * @param fileId the file id to delete
 * @return true if the operation was successful
 * @deprecated this API is currently deprecated due to Googles cloud storage deprection
 */
public boolean deleteCloudFile(String fileId) {
    if(CloudPersona.getCurrentPersona().getToken() == null) {
        CloudPersona.createAnonymous();
    }
    ConnectionRequest req = new ConnectionRequest();
    req.setPost(false);
    req.setFailSilently(true);
    req.setUrl(SERVER_URL + "/fileStoreDelete");
    req.addArgument("i", fileId);
    req.addArgument("t", CloudPersona.getCurrentPersona().getToken());
    NetworkManager.getInstance().addToQueueAndWait(req);
    if(req.getResponseCode() == 200) {
        return new String(req.getResponseData()).equals("OK");
    }
    return false;
}
 
Example 4
Source File: Message.java    From CodenameOne with GNU General Public License v2.0 6 votes vote down vote up
private ConnectionRequest createMessage(String sender, String recipient, String recipientName, String subject, String plainTextBody) {
    ConnectionRequest cr = new ConnectionRequest();
    cr.setUrl(Display.getInstance().getProperty("cloudServerURL", "https://codename-one.appspot.com/") + "sendEmailServlet");
    cr.setFailSilently(cloudMessageFailSilently);
    cr.setPost(true);
    cr.addArgument("d", Display.getInstance().getProperty("built_by_user", ""));
    cr.addArgument("from", sender);
    cr.addArgument("to", recipient);
    cr.addArgument("re", recipientName);
    cr.addArgument("subject", subject);
    if(mimeType.equals(MIME_TEXT)) {
        cr.addArgument("body", content);
    } else {
        cr.addArgument("body", plainTextBody);
        cr.addArgument("html", content);
    }
    
    return cr;
}
 
Example 5
Source File: TwitterRESTService.java    From CodenameOne with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 6
Source File: AutocompleteOverrideFilterSample.java    From CodenameOne with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example 7
Source File: MapsDemo.java    From codenameone-demos with GNU General Public License v2.0 5 votes vote down vote up
private String getDirections(Coord origin, Coord destination) throws IOException {
    ConnectionRequest req = new ConnectionRequest();
    req.setUrl("http://maps.googleapis.com/maps/api/directions/json");
    req.setUserAgent("Opera/8.0 (Windows NT 5.1; U; en)");
    req.setPost(false);
    req.addArgument("origin", "" + origin.getLatitude() + " " + origin.getLongitude());
    req.addArgument("destination", "" + destination.getLatitude() + " " + destination.getLongitude());
    req.addArgument("mode", "walking");
    req.addArgument("sensor", "false");
    NetworkManager.getInstance().addToQueueAndWait(req);
    JSONParser p = new JSONParser();
    Hashtable h = p.parse(new InputStreamReader(new ByteArrayInputStream(req.getResponseData())));
    System.out.println(h.toString());
    return ((Hashtable) ((Hashtable) ((Vector) h.get("routes")).firstElement()).get("overview_polyline")).get("points").toString();
}
 
Example 8
Source File: FaceBookAccess.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Some simple queries for public data can work just fine with anonymous login without requiring the whole
 * OAuth process, they still need a facebook application though
 * @param appid the id of the application
 * @param clientSecret the client secret for the application
 */
public static void anonymousLogin(String appid, String clientSecret) {
    ConnectionRequest req = new ConnectionRequest();
    req.setPost(false);
    req.setUrl("https://graph.facebook.com/oauth/access_token");
    req.addArgument("client_id", appid);
    req.addArgument("client_secret", clientSecret);
    req.addArgument("grant_type", "client_credentials");
    NetworkManager.getInstance().addToQueueAndWait(req);
    if(req.getResponseData() != null) {
        token = new String(req.getResponseData());
        token = token.substring(token.indexOf('=') + 1);
    }
}
 
Example 9
Source File: AnalyticsService.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
/**
 * In apps mode we can send information about an exception to the analytics server
 * @param t the exception
 * @param message up to 150 character message, 
 * @param fatal is the exception fatal
 */
public static void sendCrashReport(Throwable t, String message, boolean fatal) {
    // https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide#exception
    ConnectionRequest req = GetGARequest();
    req.addArgument("t", "exception");
    System.out.println(message);
    req.addArgument("exd", message.substring(0, Math.min(message.length(), 150) - 1));
    if(fatal) {
        req.addArgument("exf", "1");
    } else {
        req.addArgument("exf", "0");
    }
    
    NetworkManager.getInstance().addToQueue(req);
}
 
Example 10
Source File: CloudPersona.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates an anonymous persona that will be unique in the cloud, NEVER logout an anonymous user!
 * @return false in case login failed e.g. due to bad network connection
 */
public static boolean createAnonymous() {
    if(instance == null) {
        getCurrentPersona();
    }
    ConnectionRequest login = new ConnectionRequest();
    login.setPost(true);
    login.setUrl(CloudStorage.SERVER_URL + "/objStoreUser");
    login.addArgument("pk", Display.getInstance().getProperty("package_name", null));
    login.addArgument("bb", Display.getInstance().getProperty("built_by_user", null));
    NetworkManager.getInstance().addToQueueAndWait(login);
    if(login.getResposeCode() != 200) {
        return false;
    }
    
    ByteArrayInputStream bi = new ByteArrayInputStream(login.getResponseData());
    DataInputStream di = new DataInputStream(bi);
    
    if(instance == null) {
        instance = new CloudPersona();
    } 
    try {
        instance.persona = di.readUTF();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    Preferences.set("CN1Persona", instance.persona);
    Preferences.set("CN1PersonaAnonymous", true);
    
    Util.cleanup(di);
    
    return true;
}
 
Example 11
Source File: CloudStorage.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Deletes all the cloud files under this user, notice that this method
 * is asynchronous and a background server process performs the actual deletion
 * @deprecated this API is currently deprecated due to Googles cloud storage deprection
 */
public void deleteAllCloudFilesForUser() {
    if(CloudPersona.getCurrentPersona().getToken() == null) {
        return;
    }
    ConnectionRequest req = new ConnectionRequest();
    req.setPost(false);
    req.setFailSilently(true);
    req.setUrl(SERVER_URL + "/purgeCloudFiles");
    req.addArgument("own", CloudPersona.getCurrentPersona().getToken());
    req.addArgument("u", Display.getInstance().getProperty("built_by_user", ""));
    NetworkManager.getInstance().addToQueue(req);
}
 
Example 12
Source File: CloudStorage.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Deletes all the cloud files before the given time stamp for the given
 * development account. Notice that this method is meant for internal use 
 * and not for distributable apps since it includes your developer account.
 * This method works in a background server process and returns immediately.
 * @param timestamp the timestamp since epoch (as in System.currentTimemillis).
 * @param developerAccount your developer email
 * @param developerPassword your developer password
 * @deprecated this API is currently deprecated due to Googles cloud storage deprection
 */
public void deleteAllCloudFilesBefore(long timestamp, String developerAccount, String developerPassword) {
    if(CloudPersona.getCurrentPersona().getToken() == null) {
        return;
    }
    ConnectionRequest req = new ConnectionRequest();
    req.setPost(false);
    req.setFailSilently(true);
    req.setUrl(SERVER_URL + "/purgeCloudFiles");
    req.addArgument("d", "" + timestamp);
    req.addArgument("u", developerAccount);
    req.addArgument("p", developerPassword);
    NetworkManager.getInstance().addToQueue(req);
}
 
Example 13
Source File: InnerActive.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
private static void addParam(ConnectionRequest req, String key, String val) {
    if (val != null && val.length() > 0) {
        req.addArgument(key, val);
    }
}
 
Example 14
Source File: CloudPersona.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Creates a new user if a user isn't occupying the given login already, 
 * if the user exists performs a login operation.
 * 
 * @param login a user name
 * @param password a password
 * @return true if the login is successful false otherwise
 */
public static boolean createOrLogin(String login, String password) {
    if(instance == null) {
        getCurrentPersona();
        if(instance.persona != null) {
            return true;
        }
    }
    ConnectionRequest loginRequest = new ConnectionRequest();
    loginRequest.setPost(true);
    loginRequest.setUrl(CloudStorage.SERVER_URL + "/objStoreUser");
    loginRequest.addArgument("l", login);
    loginRequest.addArgument("p", password);
    loginRequest.addArgument("pk", Display.getInstance().getProperty("package_name", null));
    loginRequest.addArgument("bb", Display.getInstance().getProperty("built_by_user", null));
    NetworkManager.getInstance().addToQueueAndWait(loginRequest);
    if(loginRequest.getResposeCode() != 200) {
        return false;
    }
    
    ByteArrayInputStream bi = new ByteArrayInputStream(loginRequest.getResponseData());
    DataInputStream di = new DataInputStream(bi);
    
    try {
        if(di.readBoolean()) {
            if(instance == null) {
                instance = new CloudPersona();
            } 
            instance.persona = di.readUTF();
            Preferences.set("CN1Persona", instance.persona);
            Util.cleanup(di);
        } else {
            Util.cleanup(di);
            return false;
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    return true;
}