Send JAVA » History » Version 1
  Tomek Dziemidowicz, 2019-07-18 09:07 PM 
  
| 1 | 1 | Tomek Dziemidowicz | h1. Send JAVA | 
|---|---|---|---|
| 2 | |||
| 3 | <pre><code class="java"> | ||
| 4 | /** | ||
| 5 | * Send local changes to webservice | ||
| 6 | * @param subscriberId id of subscriber | ||
| 7 | * @throws Exception | ||
| 8 | */ | ||
| 9 | private void sendLocalChanges(String subscriberId) throws Exception { | ||
| 10 | String query; | ||
| 11 | Cursor cursor = null; | ||
| 12 | SQLiteDatabase db = null; | ||
| 13 | |||
| 14 | StringBuilder builder = new StringBuilder(); | ||
| 15 | |||
| 16 |     try { | ||
| 17 | db = openOrCreateDatabase(_dbFileName, null); | ||
| 18 | |||
| 19 | List<String> tables = new ArrayList<String>(); | ||
| 20 | query = "select tbl_Name from sqlite_master where type='table' and sql like '%RowId%' and tbl_Name != 'android_metadata';"; | ||
| 21 | cursor = db.rawQuery(query, null); | ||
| 22 |         while (cursor.moveToNext()) { | ||
| 23 | tables.add(cursor.getString(0)); | ||
| 24 | } | ||
| 25 | cursor.close(); | ||
| 26 | |||
| 27 |         builder.append("<?xml version=\"1.0\" encoding=\"utf-8\"?><SyncData xmlns=\"urn:sync-schema\">"); | ||
| 28 | |||
| 29 |         for (String tableName : tables) { | ||
| 30 |             if (!tableName.equalsIgnoreCase("MergeDelete")) { | ||
| 31 |                 builder.append(String.format("<tab n=\"%1$s\">", tableName)); | ||
| 32 | |||
| 33 |                 builder.append("<ins>"); | ||
| 34 |                 query = String.format("select * from %1$s where RowId is null;", tableName); | ||
| 35 | cursor = db.rawQuery(query, null); | ||
| 36 |                 while (cursor.moveToNext()) { | ||
| 37 |                     builder.append("<r>"); | ||
| 38 |                     for (int i = 0; i < cursor.getColumnCount(); i++) { | ||
| 39 |                         if (!cursor.getColumnName(i).equalsIgnoreCase("MergeUpdate")) { | ||
| 40 | builder.append( | ||
| 41 |                                     String.format("<%1$s><![CDATA[%2$s]]></%1$s>", | ||
| 42 | cursor.getColumnName(i), | ||
| 43 | cursor.getString(i))); | ||
| 44 | } | ||
| 45 | } | ||
| 46 |                     builder.append("</r>"); | ||
| 47 | } | ||
| 48 | cursor.close(); | ||
| 49 |                 builder.append("</ins>"); | ||
| 50 | |||
| 51 |                 builder.append("<upd>"); | ||
| 52 |                 query = String.format("select * from %1$s where MergeUpdate > 0 and RowId is not null;", tableName); | ||
| 53 | cursor = db.rawQuery(query, null); | ||
| 54 |                 while (cursor.moveToNext()) { | ||
| 55 |                     builder.append("<r>"); | ||
| 56 |                     for (int i = 0; i < cursor.getColumnCount(); i++) { | ||
| 57 |                         if (!cursor.getColumnName(i).equalsIgnoreCase("MergeUpdate")) { | ||
| 58 | builder.append( | ||
| 59 |                                     String.format("<%1$s><![CDATA[%2$s]]></%1$s>", | ||
| 60 | cursor.getColumnName(i), | ||
| 61 | cursor.getString(i))); | ||
| 62 | } | ||
| 63 | } | ||
| 64 |                     builder.append("</r>"); | ||
| 65 | } | ||
| 66 | cursor.close(); | ||
| 67 |                 builder.append("</upd>"); | ||
| 68 | |||
| 69 |                 builder.append("</tab>"); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 |         builder.append("<delete>"); | ||
| 74 |         query = String.format("select TableId,RowId from MergeDelete;"); | ||
| 75 | cursor = db.rawQuery(query, null); | ||
| 76 |         while (cursor.moveToNext()) { | ||
| 77 | builder.append( | ||
| 78 |                     String.format("<r><tb>%1$s</tb><id>%2$s</id></r>", | ||
| 79 | cursor.getString(0), | ||
| 80 | cursor.getString(1))); | ||
| 81 | } | ||
| 82 | cursor.close(); | ||
| 83 |         builder.append("</delete>"); | ||
| 84 | |||
| 85 |         builder.append("</SyncData>"); | ||
| 86 | } | ||
| 87 |     finally { | ||
| 88 |         if(cursor != null && !cursor.isClosed()){ | ||
| 89 | cursor.close(); | ||
| 90 | } | ||
| 91 |         if(db != null && db.isOpen()){ | ||
| 92 | db.close(); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | HttpURLConnection connection = null; | ||
| 97 | InputStream resultStream = null; | ||
| 98 | String resultString = null; | ||
| 99 | |||
| 100 |     String requestUrl = String.format("%s/Send", _serverURL); | ||
| 101 | JSONObject inputObject = new JSONObject(); | ||
| 102 | |||
| 103 |     try { | ||
| 104 |         inputObject.put("subscriber", subscriberId); | ||
| 105 |         inputObject.put("content", builder.toString()); | ||
| 106 |         inputObject.put("version", "3"); | ||
| 107 |         byte[] requestBytes = inputObject.toString().getBytes("UTF-8"); | ||
| 108 | |||
| 109 | connection = (HttpURLConnection) new URL(requestUrl).openConnection(); | ||
| 110 |         connection.setRequestMethod("POST"); | ||
| 111 |         connection.setRequestProperty("Content-Type", "application/json"); | ||
| 112 |         connection.setRequestProperty("charset", "utf-8"); | ||
| 113 |         connection.setRequestProperty("Content-Length", Integer.toString(requestBytes.length)); | ||
| 114 | |||
| 115 | DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); | ||
| 116 | wr.write(requestBytes); | ||
| 117 | wr.flush(); | ||
| 118 | wr.close(); | ||
| 119 | |||
| 120 | int status = connection.getResponseCode(); | ||
| 121 | |||
| 122 |         switch (status){ | ||
| 123 | case HttpURLConnection.HTTP_OK: | ||
| 124 | resultStream = connection.getInputStream(); | ||
| 125 | resultString = IOUtils.toString(resultStream, "UTF-8"); | ||
| 126 | break; | ||
| 127 | case HttpURLConnection.HTTP_NO_CONTENT: | ||
| 128 | break; | ||
| 129 | default: | ||
| 130 | resultStream = connection.getErrorStream(); | ||
| 131 | resultString = IOUtils.toString(resultStream, "UTF-8"); | ||
| 132 | throw new Exception(resultString); | ||
| 133 | } | ||
| 134 | } | ||
| 135 |     finally { | ||
| 136 |         if (resultStream != null) { | ||
| 137 |             try { | ||
| 138 | resultStream.close(); | ||
| 139 |             } catch (IOException e) { } | ||
| 140 | } | ||
| 141 |         if (connection != null) { | ||
| 142 | connection.disconnect(); | ||
| 143 | } | ||
| 144 | } | ||
| 145 | } | ||
| 146 | </code></pre> |