Project

General

Profile

Actions

Send JAVA

/**
 * Send local changes to webservice
 * @param subscriberId id of subscriber
 * @throws Exception
 */
private void sendLocalChanges(String subscriberId) throws Exception {
    String query;
    Cursor cursor = null;
    SQLiteDatabase db = null;

    StringBuilder builder = new StringBuilder();

    try {
        db = openOrCreateDatabase(_dbFileName, null);

        List<String> tables = new ArrayList<String>();
        query = "select tbl_Name from sqlite_master where type='table' and sql like '%RowId%'  and tbl_Name != 'android_metadata';";
        cursor = db.rawQuery(query, null);
        while (cursor.moveToNext()) {
            tables.add(cursor.getString(0));
        }
        cursor.close();

        builder.append("<?xml version=\"1.0\" encoding=\"utf-8\"?><SyncData xmlns=\"urn:sync-schema\">");

        for (String tableName : tables) {
            if (!tableName.equalsIgnoreCase("MergeDelete")) {
                builder.append(String.format("<tab n=\"%1$s\">", tableName));

                builder.append("<ins>");
                query = String.format("select * from %1$s where RowId is null;", tableName);
                cursor = db.rawQuery(query, null);
                while (cursor.moveToNext()) {
                    builder.append("<r>");
                    for (int i = 0; i < cursor.getColumnCount(); i++) {
                        if (!cursor.getColumnName(i).equalsIgnoreCase("MergeUpdate")) {
                            builder.append(
                                    String.format("<%1$s><![CDATA[%2$s]]></%1$s>",
                                            cursor.getColumnName(i),
                                            cursor.getString(i)));
                        }
                    }
                    builder.append("</r>");
                }
                cursor.close();
                builder.append("</ins>");

                builder.append("<upd>");
                query = String.format("select * from %1$s where MergeUpdate > 0 and RowId is not null;", tableName);
                cursor = db.rawQuery(query, null);
                while (cursor.moveToNext()) {
                    builder.append("<r>");
                    for (int i = 0; i < cursor.getColumnCount(); i++) {
                        if (!cursor.getColumnName(i).equalsIgnoreCase("MergeUpdate")) {
                            builder.append(
                                    String.format("<%1$s><![CDATA[%2$s]]></%1$s>",
                                            cursor.getColumnName(i),
                                            cursor.getString(i)));
                        }
                    }
                    builder.append("</r>");
                }
                cursor.close();
                builder.append("</upd>");

                builder.append("</tab>");
            }
        }

        builder.append("<delete>");
        query = String.format("select TableId,RowId from MergeDelete;");
        cursor = db.rawQuery(query, null);
        while (cursor.moveToNext()) {
            builder.append(
                    String.format("<r><tb>%1$s</tb><id>%2$s</id></r>",
                            cursor.getString(0),
                            cursor.getString(1)));
        }
        cursor.close();
        builder.append("</delete>");

        builder.append("</SyncData>");
    }
    finally {
        if(cursor != null && !cursor.isClosed()){
            cursor.close();
        }
        if(db != null && db.isOpen()){
            db.close();
        }
    }

    HttpURLConnection connection = null;
    InputStream resultStream = null;
    String resultString = null;

    String requestUrl = String.format("%s/Send", _serverURL);
    JSONObject inputObject = new JSONObject();

    try {
        inputObject.put("subscriber", subscriberId);
        inputObject.put("content", builder.toString());
        inputObject.put("version", "3");
        byte[] requestBytes = inputObject.toString().getBytes("UTF-8");

        connection = (HttpURLConnection) new URL(requestUrl).openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("charset", "utf-8");
        connection.setRequestProperty("Content-Length", Integer.toString(requestBytes.length));

        DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
        wr.write(requestBytes);
        wr.flush();
        wr.close();

        int status = connection.getResponseCode();

        switch (status){
            case HttpURLConnection.HTTP_OK:
                resultStream = connection.getInputStream();
                resultString = IOUtils.toString(resultStream, "UTF-8");
                break;
            case HttpURLConnection.HTTP_NO_CONTENT:
                break;
            default:
                resultStream = connection.getErrorStream();
                resultString = IOUtils.toString(resultStream, "UTF-8");
                throw new Exception(resultString);
        }
    }
    finally {
        if (resultStream != null) {
            try {
                resultStream.close();
            } catch (IOException e) { }
        }
        if (connection != null) {
            connection.disconnect();
        }
    }
}

Updated by Tomek Dziemidowicz almost 5 years ago · 1 revisions

Go to top