Skip to content

Commit 2be5ab8

Browse files
Handle null at server end, gson builder for null
1 parent 0ea965a commit 2be5ab8

File tree

5 files changed

+14
-4
lines changed

5 files changed

+14
-4
lines changed

app/src/main/java/com/sample/MainActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ protected void onCreate(Bundle savedInstanceState) {
7373
String email = "email_" + i;
7474
String street = "street_" + i;
7575
String place = "place_" + i;
76-
contactDBHelper.insertContact(name, phone, email, street, place);
76+
contactDBHelper.insertContact(name, phone, email, street, null);
7777
}
7878
}
7979

debug-db/src/main/java/com/amitshekhar/server/RequestHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.amitshekhar.utils.PrefHelper;
3636
import com.amitshekhar.utils.Utils;
3737
import com.google.gson.Gson;
38+
import com.google.gson.GsonBuilder;
3839
import com.google.gson.reflect.TypeToken;
3940

4041
import java.io.BufferedReader;
@@ -64,7 +65,7 @@ public class RequestHandler {
6465
public RequestHandler(Context context) {
6566
mContext = context;
6667
mAssets = context.getResources().getAssets();
67-
mGson = new Gson();
68+
mGson = new GsonBuilder().serializeNulls().create();
6869
}
6970

7071
public void handle(Socket socket) throws IOException {

debug-db/src/main/java/com/amitshekhar/utils/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private Constants() {
3131

3232
public static final String APP_SHARED_PREFERENCES = "APP_SHARED_PREFERENCES";
3333
public static final String PK = "pk";
34-
public static final String TYPE = "type";
3534
public static final String NAME = "name";
35+
public static final String NULL = "null";
3636

3737
}

debug-db/src/main/java/com/amitshekhar/utils/DatabaseHelper.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ public static UpdateRowResponse updateRow(SQLiteDatabase db, String tableName, L
205205
List<String> whereArgsList = new ArrayList<>();
206206

207207
for (RowDataRequest rowDataRequest : rowDataRequests) {
208+
if (Constants.NULL.equals(rowDataRequest.value)) {
209+
rowDataRequest.value = null;
210+
}
208211
if (rowDataRequest.isPrimary) {
209212
if (whereClause == null) {
210213
whereClause = rowDataRequest.title + "=? ";
@@ -254,6 +257,9 @@ public static UpdateRowResponse deleteRow(SQLiteDatabase db, String tableName, L
254257
List<String> whereArgsList = new ArrayList<>();
255258

256259
for (RowDataRequest rowDataRequest : rowDataRequests) {
260+
if (Constants.NULL.equals(rowDataRequest.value)) {
261+
rowDataRequest.value = null;
262+
}
257263
if (rowDataRequest.isPrimary) {
258264
if (whereClause == null) {
259265
whereClause = rowDataRequest.title + "=? ";
@@ -281,7 +287,6 @@ public static UpdateRowResponse deleteRow(SQLiteDatabase db, String tableName, L
281287
}
282288

283289

284-
285290
public static TableDataResponse exec(SQLiteDatabase database, String sql) {
286291
TableDataResponse tableDataResponse = new TableDataResponse();
287292
tableDataResponse.isSelectQuery = false;

debug-db/src/main/java/com/amitshekhar/utils/PrefHelper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ public static UpdateRowResponse updateRow(Context context, String tableName, Lis
155155
String value = rowDataValue.value;
156156
String dataType = rowDataValue.dataType;
157157

158+
if (Constants.NULL.equals(value)) {
159+
value = null;
160+
}
161+
158162
SharedPreferences preferences = context.getSharedPreferences(tableName, Context.MODE_PRIVATE);
159163

160164
try {

0 commit comments

Comments
 (0)