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

The following examples show how to use com.codename1.io.ConnectionRequest#getResponseData() . 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: SignIn.java    From codenameone-demos with GNU General Public License v2.0 6 votes vote down vote up
private void showFacebookUser(String token){
    ConnectionRequest req = new ConnectionRequest();
    req.setPost(false);
    req.setUrl("https://graph.facebook.com/v2.3/me");
    req.addArgumentNoEncoding("access_token", token);
    InfiniteProgress ip = new InfiniteProgress();
    Dialog d = ip.showInifiniteBlocking();
    NetworkManager.getInstance().addToQueueAndWait(req);
    byte[] data = req.getResponseData();
    JSONParser parser = new JSONParser();
    Map map = null;
    try {
        map = parser.parseJSON(new InputStreamReader(new ByteArrayInputStream(data), "UTF-8"));
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    String name = (String) map.get("name");
    d.dispose();
    Form userForm = new UserForm(name, (EncodedImage) theme.getImage("user.png"), "https://graph.facebook.com/v2.3/me/picture?access_token=" + token);
    userForm.show();
}
 
Example 3
Source File: SignIn.java    From codenameone-demos with GNU General Public License v2.0 6 votes vote down vote up
private void showGoogleUser(String token){
    ConnectionRequest req = new ConnectionRequest();
    req.addRequestHeader("Authorization", "Bearer " + token);
    req.setUrl("https://www.googleapis.com/plus/v1/people/me");
    req.setPost(false);
    InfiniteProgress ip = new InfiniteProgress();
    Dialog d = ip.showInifiniteBlocking();
    NetworkManager.getInstance().addToQueueAndWait(req);
    d.dispose();
    byte[] data = req.getResponseData();
    JSONParser parser = new JSONParser();
    Map map = null;
    try {
        map = parser.parseJSON(new InputStreamReader(new ByteArrayInputStream(data), "UTF-8"));
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    String name = (String) map.get("displayName");
    Map im = (Map) map.get("image");
    String url = (String) im.get("url");
    Form userForm = new UserForm(name, (EncodedImage) theme.getImage("user.png"), url);
    userForm.show();
}
 
Example 4
Source File: RequestBuilder.java    From CodenameOne with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Executes the request synchronously
 * 
 * @return Response Object
 */ 
public Response<String> getAsString() {
    ConnectionRequest request = createRequest(false);
    fetched = true;
    CN.addToQueueAndWait(request);
    Response res = null;
    try {
        byte[] respData = request.getResponseData();
        String resp = null;
        if(respData != null) {
            resp = new String(respData, "UTF-8");
        }
        res = new Response(request.getResponseCode(), resp, 
            request.getResponseErrorMessage());
    } catch (UnsupportedEncodingException ex) {
        ex.printStackTrace();
    }
    return res;
}
 
Example 5
Source File: SignIn.java    From CodenameOne with GNU General Public License v2.0 6 votes vote down vote up
private void showFacebookUser(String token){
    ConnectionRequest req = new ConnectionRequest();
    req.setPost(false);
    req.setUrl("https://graph.facebook.com/v2.3/me");
    req.addArgumentNoEncoding("access_token", token);
    InfiniteProgress ip = new InfiniteProgress();
    Dialog d = ip.showInifiniteBlocking();
    NetworkManager.getInstance().addToQueueAndWait(req);
    byte[] data = req.getResponseData();
    JSONParser parser = new JSONParser();
    Map map = null;
    try {
        map = parser.parseJSON(new InputStreamReader(new ByteArrayInputStream(data), "UTF-8"));
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    String name = (String) map.get("name");
    d.dispose();
    Form userForm = new UserForm(name, (EncodedImage) theme.getImage("user.png"), "https://graph.facebook.com/v2.3/me/picture?access_token=" + token);
    userForm.show();
}
 
Example 6
Source File: SignIn.java    From CodenameOne with GNU General Public License v2.0 6 votes vote down vote up
private void showGoogleUser(String token){
    ConnectionRequest req = new ConnectionRequest();
    req.addRequestHeader("Authorization", "Bearer " + token);
    req.setUrl("https://www.googleapis.com/plus/v1/people/me");
    req.setPost(false);
    InfiniteProgress ip = new InfiniteProgress();
    Dialog d = ip.showInifiniteBlocking();
    NetworkManager.getInstance().addToQueueAndWait(req);
    d.dispose();
    byte[] data = req.getResponseData();
    JSONParser parser = new JSONParser();
    Map map = null;
    try {
        map = parser.parseJSON(new InputStreamReader(new ByteArrayInputStream(data), "UTF-8"));
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    String name = (String) map.get("displayName");
    Map im = (Map) map.get("image");
    String url = (String) im.get("url");
    Form userForm = new UserForm(name, (EncodedImage) theme.getImage("user.png"), url);
    userForm.show();
}
 
Example 7
Source File: ParseResponse.java    From parse4cn1 with Apache License 2.0 5 votes vote down vote up
/**
 * Extracts the response from the provided {@code request}.
 *
 * @param request The (executed) network request.
 */
protected void extractResponseData(final ConnectionRequest request) {
    if (request.getResponseData() != null) {
        responseBody = request.getResponseData();
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Raw response (as string): " + new String(responseBody));
        }
        setStatusCode(request.getResponseCode());
    }
}
 
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: 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 10
Source File: RequestBuilder.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Executes the request synchronously
 * 
 * @return Response Object
 */ 
public Response<byte[]> getAsBytes() {
    ConnectionRequest request = createRequest(false);
    fetched = true;
    CN.addToQueueAndWait(request);
    Response res = new Response(request.getResponseCode(), request.getResponseData(), request.getResponseErrorMessage());
    return res;
}
 
Example 11
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;
}
 
Example 12
Source File: CloudStorage.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
private String uploadCloudFileImpl(String mimeType, String file, InputStream data, int dataSize) throws CloudException, IOException {
    String token = CloudPersona.getCurrentPersona().getToken();
    if(token == null || token.length() == 0) {
        if(!CloudPersona.createAnonymous()) {
            throw new CloudException(RETURN_CODE_FAIL_SERVER_ERROR, "Error creating anonymous login");
        }
        token = CloudPersona.getCurrentPersona().getToken();
    }
    ConnectionRequest req = new ConnectionRequest();
    req.setPost(false);
    req.setUrl(SERVER_URL + "/fileStoreURLRequest");
    //req.addArgument("bb", Display.getInstance().getProperty("built_by_user", null));

    NetworkManager.getInstance().addToQueueAndWait(req);
    int rc = req.getResponseCode();
    if(rc != 200) {
        if(rc == 420) {
            throw new CloudException(RETURN_CODE_FAIL_QUOTA_EXCEEDED);
        }
        throw new CloudException(RETURN_CODE_FAIL_SERVER_ERROR);
    }

    String d = new String(req.getResponseData());
    MultipartRequest uploadReq = new MultipartRequest();
    uploadReq.setUrl(d);
    uploadReq.setManualRedirect(false);
    uploadReq.addArgument("bb", Display.getInstance().getProperty("built_by_user", null));
    uploadReq.addArgument("t", CloudPersona.getCurrentPersona().getToken());
    uploadReq.addArgument("pk", Display.getInstance().getProperty("package_name", null));
    if(data == null) {
        int pos = file.lastIndexOf('/');
        String shortName = file;
        if(pos > -1) {
            shortName = file.substring(pos);
        }
        uploadReq.addData(shortName, file, mimeType);
    } else {
        uploadReq.addData(file, data, dataSize, mimeType);
    }
    NetworkManager.getInstance().addToQueueAndWait(uploadReq);
    if(uploadReq.getResponseCode() != 200) {
        throw new CloudException(RETURN_CODE_FAIL_SERVER_ERROR);
    }
    String r = new String(uploadReq.getResponseData());
    if("ERROR".equals(r)) {
        throw new CloudException(RETURN_CODE_FAIL_SERVER_ERROR);
    }
    return r;
}