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

The following examples show how to use com.codename1.io.ConnectionRequest#getResponseCode() . 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: 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 2
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 3
Source File: RequestBuilder.java    From CodenameOne with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Executes the request synchronously
 * 
 * @param type the type of the business object to create
 * @return Response Object
 */ 
public Response<List<PropertyBusinessObject>> getAsPropertyList(Class type) {
    ConnectionRequest request = createRequest(true);
    fetched = true;
    CN.addToQueueAndWait(request);
    Map response = ((Connection)request).json;
    try {
        List<Map> lst = (List<Map>)response.get("root");
        List<PropertyBusinessObject> result = new ArrayList<PropertyBusinessObject>();
        for(Map m : lst) {
            PropertyBusinessObject pb = (PropertyBusinessObject)type.newInstance();
            pb.getPropertyIndex().populateFromMap(m);
            result.add(pb);
        }
        return new Response(request.getResponseCode(), result, request.getResponseErrorMessage());
    } catch(Exception err) {
        Log.e(err);
        throw new RuntimeException(err.toString());
    }
}
 
Example 4
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 5
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<Map> getAsJsonMap() {
    ConnectionRequest request = createRequest(true);
    fetched = true;
    CN.addToQueueAndWait(request);
    Map response = ((Connection)request).json;
    return new Response(request.getResponseCode(), response, request.getResponseErrorMessage());
}
 
Example 6
Source File: RequestBuilder.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Executes the request synchronously
 * 
 * @param type the type of the business object to create
 * @return Response Object
 */ 
public Response<PropertyBusinessObject> getAsProperties(Class type) {
    ConnectionRequest request = createRequest(true);
    fetched = true;
    CN.addToQueueAndWait(request);
    Map response = ((Connection)request).json;
    try {
        PropertyBusinessObject pb = (PropertyBusinessObject)type.newInstance();
        pb.getPropertyIndex().populateFromMap(response);
        return new Response(request.getResponseCode(), pb, request.getResponseErrorMessage());
    } catch(Exception err) {
        Log.e(err);
        throw new RuntimeException(err.toString());
    }
}
 
Example 7
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;
}