Sending and receiving of JSON data to and from Android Studio
$begingroup$
I'm currently developing an app that requires sending and receiving of data from android studio going to MySQL and then data coming from MySQL will be saved from the SQLite. I need advice on how to make the process faster. Right now I'm inserting 80,000 + rows of data coming from MySQL and then saving it to SQLite and that process lasts around 25-30 minutes.
rowItem.php
<?php
require "init.php";
$serial = $_POST['mySerial'];
$sql = "select ITEMCODE, DESCRIPTION, BRAND from items where SERIAL_NO = '" .$serial. "'";
$result = mysqli_query($con, $sql);
$data =array();
while($row = mysqli_fetch_array($result)) {
$row['ITEMCODE'] = mb_convert_encoding($row['ITEMCODE'], 'UTF-8', 'UTF-8');
$row['DESCRIPTION'] = mb_convert_encoding($row['DESCRIPTION'], 'UTF-8', 'UTF-8');
$row['BRAND'] = mb_convert_encoding($row['BRAND'], 'UTF-8', 'UTF-8');
array_push($data, array('ITEMCODE' => $row['ITEMCODE'], 'DESCRIPTION' => $row['DESCRIPTION'], 'BRAND' => $row['BRAND']));
}
$json = json_encode(array("allItems"=>$data));
echo $json;
?>
DatabaseOperations.java
public class DatabaseOperations extends SQLiteOpenHelper {
public static final int dbVersion = 1;
public String CREATE_ITEMS_TABLE = "CREATE TABLE " + TableData.TableInfo.TB_ITEMS +
" (" + TableData.TableInfo.COL_ITEMS_ITEMCODE + " VARCHAR(20) PRIMARY KEY NOT NULL, " +
TableData.TableInfo.COL_ITEMS_DESCRIPTION + " VARCHAR(50), " +
TableData.TableInfo.COL_ITEMS_BRAND + " VARCHAR(10), " +
TableData.TableInfo.COL_ITEMS_BARCODE + " VARCHAR(20));";
public DatabaseOperations(Context context) {
super(context, TableData.TableInfo.DB_NAME, null, dbVersion);
}
@Override
public void onCreate(SQLiteDatabase sdb) {
sdb.execSQL(CREATE_ITEMS_TABLE);
}
public void insertItems (DatabaseOperations dop,
String itemCode, String brand, String desc) {
SQLiteDatabase sq = dop.getWritableDatabase();
sq.beginTransaction();
try {
ContentValues cv = new ContentValues();
cv.put(TableData.TableInfo.COL_ITEMS_ITEMCODE, itemCode);
cv.put(TableData.TableInfo.COL_ITEMS_BRAND, brand);
cv.put(TableData.TableInfo.COL_ITEMS_DESCRIPTION, desc);
sq.insert(TableData.TableInfo.TB_ITEMS, null, cv);
sq.setTransactionSuccessful();
}
catch (Exception e) {
}
finally {
sq.endTransaction();
sq.close();
}
}
}
MainScreen.java
public class MainScreen extends AppCompatActivity {
Context ctx = this;
Button btnSync;
String rowItemURL = "http://192.168.100.118:81/rowItem.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
btnSync = findViewById(R.id.btnSync);
btnSync.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringRequest itemImeiRequest = new StringRequest(Request.Method.POST, rowItemURL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject;
jsonObject = new JSONObject(response);
JSONArray itemArray = jsonObject.getJSONArray("allItems");
for (int i = 0; i < itemArray.length(); i++) {
itemCode = itemArray.getJSONObject(i).getString("ITEMCODE");
itemBrand = itemArray.getJSONObject(i).getString("BRAND");
itemDesc = itemArray.getJSONObject(i).getString("DESCRIPTION");
DatabaseOperations dop = new DatabaseOperations(ctx);
dop.insertItems(dop, itemCode, itemBrand, itemDesc);
}
Toast.makeText(ctx, "SYNC ITEMS COMPLETED!", Toast.LENGTH_LONG).show();
lblDebug.setText("SUCCESS!");
}
catch (JSONException e) {
Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_LONG).show();
lblDebug.setText(e.getMessage() + "500");
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(ctx, error.getMessage(), Toast.LENGTH_LONG).show();
lblDebug.setText(error.getMessage() + "511");
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
SharedPreferences sharedPreferencesIMEI = getSharedPreferences(IMEI_PREF, MODE_PRIVATE);
myIMEI = sharedPreferencesIMEI.getString(TEXT5, "");
Map<String,String> params = new HashMap<>();
params.put("mySerial", myIMEI);
return params;
}
};
MySingleton.getInstance(MainScreen.this).addToRequestQue(itemImeiRequest);
}
}
}
}
MySingleton.java
public class MySingleton {
private static MySingleton mInstance;
private RequestQueue requestQueue;
private static Context mCtx;
private MySingleton(Context context) {
mCtx = context;
requestQueue = getRequestQueue();
}
public RequestQueue getRequestQueue(){
if(requestQueue==null) {
requestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
}
return requestQueue;
}
public static synchronized MySingleton getInstance(Context context) {
if(mInstance==null) {
mInstance = new MySingleton(context);
}
return mInstance;
}
public<T> void addToRequestQue(Request<T> request) {
requestQueue.add(request);
}
}
java php android mysql sqlite
$endgroup$
migrated from stackoverflow.com 24 mins ago
This question came from our site for professional and enthusiast programmers.
add a comment |
$begingroup$
I'm currently developing an app that requires sending and receiving of data from android studio going to MySQL and then data coming from MySQL will be saved from the SQLite. I need advice on how to make the process faster. Right now I'm inserting 80,000 + rows of data coming from MySQL and then saving it to SQLite and that process lasts around 25-30 minutes.
rowItem.php
<?php
require "init.php";
$serial = $_POST['mySerial'];
$sql = "select ITEMCODE, DESCRIPTION, BRAND from items where SERIAL_NO = '" .$serial. "'";
$result = mysqli_query($con, $sql);
$data =array();
while($row = mysqli_fetch_array($result)) {
$row['ITEMCODE'] = mb_convert_encoding($row['ITEMCODE'], 'UTF-8', 'UTF-8');
$row['DESCRIPTION'] = mb_convert_encoding($row['DESCRIPTION'], 'UTF-8', 'UTF-8');
$row['BRAND'] = mb_convert_encoding($row['BRAND'], 'UTF-8', 'UTF-8');
array_push($data, array('ITEMCODE' => $row['ITEMCODE'], 'DESCRIPTION' => $row['DESCRIPTION'], 'BRAND' => $row['BRAND']));
}
$json = json_encode(array("allItems"=>$data));
echo $json;
?>
DatabaseOperations.java
public class DatabaseOperations extends SQLiteOpenHelper {
public static final int dbVersion = 1;
public String CREATE_ITEMS_TABLE = "CREATE TABLE " + TableData.TableInfo.TB_ITEMS +
" (" + TableData.TableInfo.COL_ITEMS_ITEMCODE + " VARCHAR(20) PRIMARY KEY NOT NULL, " +
TableData.TableInfo.COL_ITEMS_DESCRIPTION + " VARCHAR(50), " +
TableData.TableInfo.COL_ITEMS_BRAND + " VARCHAR(10), " +
TableData.TableInfo.COL_ITEMS_BARCODE + " VARCHAR(20));";
public DatabaseOperations(Context context) {
super(context, TableData.TableInfo.DB_NAME, null, dbVersion);
}
@Override
public void onCreate(SQLiteDatabase sdb) {
sdb.execSQL(CREATE_ITEMS_TABLE);
}
public void insertItems (DatabaseOperations dop,
String itemCode, String brand, String desc) {
SQLiteDatabase sq = dop.getWritableDatabase();
sq.beginTransaction();
try {
ContentValues cv = new ContentValues();
cv.put(TableData.TableInfo.COL_ITEMS_ITEMCODE, itemCode);
cv.put(TableData.TableInfo.COL_ITEMS_BRAND, brand);
cv.put(TableData.TableInfo.COL_ITEMS_DESCRIPTION, desc);
sq.insert(TableData.TableInfo.TB_ITEMS, null, cv);
sq.setTransactionSuccessful();
}
catch (Exception e) {
}
finally {
sq.endTransaction();
sq.close();
}
}
}
MainScreen.java
public class MainScreen extends AppCompatActivity {
Context ctx = this;
Button btnSync;
String rowItemURL = "http://192.168.100.118:81/rowItem.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
btnSync = findViewById(R.id.btnSync);
btnSync.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringRequest itemImeiRequest = new StringRequest(Request.Method.POST, rowItemURL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject;
jsonObject = new JSONObject(response);
JSONArray itemArray = jsonObject.getJSONArray("allItems");
for (int i = 0; i < itemArray.length(); i++) {
itemCode = itemArray.getJSONObject(i).getString("ITEMCODE");
itemBrand = itemArray.getJSONObject(i).getString("BRAND");
itemDesc = itemArray.getJSONObject(i).getString("DESCRIPTION");
DatabaseOperations dop = new DatabaseOperations(ctx);
dop.insertItems(dop, itemCode, itemBrand, itemDesc);
}
Toast.makeText(ctx, "SYNC ITEMS COMPLETED!", Toast.LENGTH_LONG).show();
lblDebug.setText("SUCCESS!");
}
catch (JSONException e) {
Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_LONG).show();
lblDebug.setText(e.getMessage() + "500");
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(ctx, error.getMessage(), Toast.LENGTH_LONG).show();
lblDebug.setText(error.getMessage() + "511");
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
SharedPreferences sharedPreferencesIMEI = getSharedPreferences(IMEI_PREF, MODE_PRIVATE);
myIMEI = sharedPreferencesIMEI.getString(TEXT5, "");
Map<String,String> params = new HashMap<>();
params.put("mySerial", myIMEI);
return params;
}
};
MySingleton.getInstance(MainScreen.this).addToRequestQue(itemImeiRequest);
}
}
}
}
MySingleton.java
public class MySingleton {
private static MySingleton mInstance;
private RequestQueue requestQueue;
private static Context mCtx;
private MySingleton(Context context) {
mCtx = context;
requestQueue = getRequestQueue();
}
public RequestQueue getRequestQueue(){
if(requestQueue==null) {
requestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
}
return requestQueue;
}
public static synchronized MySingleton getInstance(Context context) {
if(mInstance==null) {
mInstance = new MySingleton(context);
}
return mInstance;
}
public<T> void addToRequestQue(Request<T> request) {
requestQueue.add(request);
}
}
java php android mysql sqlite
$endgroup$
migrated from stackoverflow.com 24 mins ago
This question came from our site for professional and enthusiast programmers.
1
$begingroup$
Please read about SQL injection. Instead of building queries with string concatenation, use prepared statements with bound parameters. See this page and this post for some good examples.
$endgroup$
– John Conde
4 hours ago
$begingroup$
Do it all with one transaction instead of one transaction per row inserted.
$endgroup$
– Shawn
4 hours ago
add a comment |
$begingroup$
I'm currently developing an app that requires sending and receiving of data from android studio going to MySQL and then data coming from MySQL will be saved from the SQLite. I need advice on how to make the process faster. Right now I'm inserting 80,000 + rows of data coming from MySQL and then saving it to SQLite and that process lasts around 25-30 minutes.
rowItem.php
<?php
require "init.php";
$serial = $_POST['mySerial'];
$sql = "select ITEMCODE, DESCRIPTION, BRAND from items where SERIAL_NO = '" .$serial. "'";
$result = mysqli_query($con, $sql);
$data =array();
while($row = mysqli_fetch_array($result)) {
$row['ITEMCODE'] = mb_convert_encoding($row['ITEMCODE'], 'UTF-8', 'UTF-8');
$row['DESCRIPTION'] = mb_convert_encoding($row['DESCRIPTION'], 'UTF-8', 'UTF-8');
$row['BRAND'] = mb_convert_encoding($row['BRAND'], 'UTF-8', 'UTF-8');
array_push($data, array('ITEMCODE' => $row['ITEMCODE'], 'DESCRIPTION' => $row['DESCRIPTION'], 'BRAND' => $row['BRAND']));
}
$json = json_encode(array("allItems"=>$data));
echo $json;
?>
DatabaseOperations.java
public class DatabaseOperations extends SQLiteOpenHelper {
public static final int dbVersion = 1;
public String CREATE_ITEMS_TABLE = "CREATE TABLE " + TableData.TableInfo.TB_ITEMS +
" (" + TableData.TableInfo.COL_ITEMS_ITEMCODE + " VARCHAR(20) PRIMARY KEY NOT NULL, " +
TableData.TableInfo.COL_ITEMS_DESCRIPTION + " VARCHAR(50), " +
TableData.TableInfo.COL_ITEMS_BRAND + " VARCHAR(10), " +
TableData.TableInfo.COL_ITEMS_BARCODE + " VARCHAR(20));";
public DatabaseOperations(Context context) {
super(context, TableData.TableInfo.DB_NAME, null, dbVersion);
}
@Override
public void onCreate(SQLiteDatabase sdb) {
sdb.execSQL(CREATE_ITEMS_TABLE);
}
public void insertItems (DatabaseOperations dop,
String itemCode, String brand, String desc) {
SQLiteDatabase sq = dop.getWritableDatabase();
sq.beginTransaction();
try {
ContentValues cv = new ContentValues();
cv.put(TableData.TableInfo.COL_ITEMS_ITEMCODE, itemCode);
cv.put(TableData.TableInfo.COL_ITEMS_BRAND, brand);
cv.put(TableData.TableInfo.COL_ITEMS_DESCRIPTION, desc);
sq.insert(TableData.TableInfo.TB_ITEMS, null, cv);
sq.setTransactionSuccessful();
}
catch (Exception e) {
}
finally {
sq.endTransaction();
sq.close();
}
}
}
MainScreen.java
public class MainScreen extends AppCompatActivity {
Context ctx = this;
Button btnSync;
String rowItemURL = "http://192.168.100.118:81/rowItem.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
btnSync = findViewById(R.id.btnSync);
btnSync.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringRequest itemImeiRequest = new StringRequest(Request.Method.POST, rowItemURL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject;
jsonObject = new JSONObject(response);
JSONArray itemArray = jsonObject.getJSONArray("allItems");
for (int i = 0; i < itemArray.length(); i++) {
itemCode = itemArray.getJSONObject(i).getString("ITEMCODE");
itemBrand = itemArray.getJSONObject(i).getString("BRAND");
itemDesc = itemArray.getJSONObject(i).getString("DESCRIPTION");
DatabaseOperations dop = new DatabaseOperations(ctx);
dop.insertItems(dop, itemCode, itemBrand, itemDesc);
}
Toast.makeText(ctx, "SYNC ITEMS COMPLETED!", Toast.LENGTH_LONG).show();
lblDebug.setText("SUCCESS!");
}
catch (JSONException e) {
Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_LONG).show();
lblDebug.setText(e.getMessage() + "500");
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(ctx, error.getMessage(), Toast.LENGTH_LONG).show();
lblDebug.setText(error.getMessage() + "511");
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
SharedPreferences sharedPreferencesIMEI = getSharedPreferences(IMEI_PREF, MODE_PRIVATE);
myIMEI = sharedPreferencesIMEI.getString(TEXT5, "");
Map<String,String> params = new HashMap<>();
params.put("mySerial", myIMEI);
return params;
}
};
MySingleton.getInstance(MainScreen.this).addToRequestQue(itemImeiRequest);
}
}
}
}
MySingleton.java
public class MySingleton {
private static MySingleton mInstance;
private RequestQueue requestQueue;
private static Context mCtx;
private MySingleton(Context context) {
mCtx = context;
requestQueue = getRequestQueue();
}
public RequestQueue getRequestQueue(){
if(requestQueue==null) {
requestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
}
return requestQueue;
}
public static synchronized MySingleton getInstance(Context context) {
if(mInstance==null) {
mInstance = new MySingleton(context);
}
return mInstance;
}
public<T> void addToRequestQue(Request<T> request) {
requestQueue.add(request);
}
}
java php android mysql sqlite
$endgroup$
I'm currently developing an app that requires sending and receiving of data from android studio going to MySQL and then data coming from MySQL will be saved from the SQLite. I need advice on how to make the process faster. Right now I'm inserting 80,000 + rows of data coming from MySQL and then saving it to SQLite and that process lasts around 25-30 minutes.
rowItem.php
<?php
require "init.php";
$serial = $_POST['mySerial'];
$sql = "select ITEMCODE, DESCRIPTION, BRAND from items where SERIAL_NO = '" .$serial. "'";
$result = mysqli_query($con, $sql);
$data =array();
while($row = mysqli_fetch_array($result)) {
$row['ITEMCODE'] = mb_convert_encoding($row['ITEMCODE'], 'UTF-8', 'UTF-8');
$row['DESCRIPTION'] = mb_convert_encoding($row['DESCRIPTION'], 'UTF-8', 'UTF-8');
$row['BRAND'] = mb_convert_encoding($row['BRAND'], 'UTF-8', 'UTF-8');
array_push($data, array('ITEMCODE' => $row['ITEMCODE'], 'DESCRIPTION' => $row['DESCRIPTION'], 'BRAND' => $row['BRAND']));
}
$json = json_encode(array("allItems"=>$data));
echo $json;
?>
DatabaseOperations.java
public class DatabaseOperations extends SQLiteOpenHelper {
public static final int dbVersion = 1;
public String CREATE_ITEMS_TABLE = "CREATE TABLE " + TableData.TableInfo.TB_ITEMS +
" (" + TableData.TableInfo.COL_ITEMS_ITEMCODE + " VARCHAR(20) PRIMARY KEY NOT NULL, " +
TableData.TableInfo.COL_ITEMS_DESCRIPTION + " VARCHAR(50), " +
TableData.TableInfo.COL_ITEMS_BRAND + " VARCHAR(10), " +
TableData.TableInfo.COL_ITEMS_BARCODE + " VARCHAR(20));";
public DatabaseOperations(Context context) {
super(context, TableData.TableInfo.DB_NAME, null, dbVersion);
}
@Override
public void onCreate(SQLiteDatabase sdb) {
sdb.execSQL(CREATE_ITEMS_TABLE);
}
public void insertItems (DatabaseOperations dop,
String itemCode, String brand, String desc) {
SQLiteDatabase sq = dop.getWritableDatabase();
sq.beginTransaction();
try {
ContentValues cv = new ContentValues();
cv.put(TableData.TableInfo.COL_ITEMS_ITEMCODE, itemCode);
cv.put(TableData.TableInfo.COL_ITEMS_BRAND, brand);
cv.put(TableData.TableInfo.COL_ITEMS_DESCRIPTION, desc);
sq.insert(TableData.TableInfo.TB_ITEMS, null, cv);
sq.setTransactionSuccessful();
}
catch (Exception e) {
}
finally {
sq.endTransaction();
sq.close();
}
}
}
MainScreen.java
public class MainScreen extends AppCompatActivity {
Context ctx = this;
Button btnSync;
String rowItemURL = "http://192.168.100.118:81/rowItem.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
btnSync = findViewById(R.id.btnSync);
btnSync.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringRequest itemImeiRequest = new StringRequest(Request.Method.POST, rowItemURL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject;
jsonObject = new JSONObject(response);
JSONArray itemArray = jsonObject.getJSONArray("allItems");
for (int i = 0; i < itemArray.length(); i++) {
itemCode = itemArray.getJSONObject(i).getString("ITEMCODE");
itemBrand = itemArray.getJSONObject(i).getString("BRAND");
itemDesc = itemArray.getJSONObject(i).getString("DESCRIPTION");
DatabaseOperations dop = new DatabaseOperations(ctx);
dop.insertItems(dop, itemCode, itemBrand, itemDesc);
}
Toast.makeText(ctx, "SYNC ITEMS COMPLETED!", Toast.LENGTH_LONG).show();
lblDebug.setText("SUCCESS!");
}
catch (JSONException e) {
Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_LONG).show();
lblDebug.setText(e.getMessage() + "500");
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(ctx, error.getMessage(), Toast.LENGTH_LONG).show();
lblDebug.setText(error.getMessage() + "511");
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
SharedPreferences sharedPreferencesIMEI = getSharedPreferences(IMEI_PREF, MODE_PRIVATE);
myIMEI = sharedPreferencesIMEI.getString(TEXT5, "");
Map<String,String> params = new HashMap<>();
params.put("mySerial", myIMEI);
return params;
}
};
MySingleton.getInstance(MainScreen.this).addToRequestQue(itemImeiRequest);
}
}
}
}
MySingleton.java
public class MySingleton {
private static MySingleton mInstance;
private RequestQueue requestQueue;
private static Context mCtx;
private MySingleton(Context context) {
mCtx = context;
requestQueue = getRequestQueue();
}
public RequestQueue getRequestQueue(){
if(requestQueue==null) {
requestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
}
return requestQueue;
}
public static synchronized MySingleton getInstance(Context context) {
if(mInstance==null) {
mInstance = new MySingleton(context);
}
return mInstance;
}
public<T> void addToRequestQue(Request<T> request) {
requestQueue.add(request);
}
}
java php android mysql sqlite
java php android mysql sqlite
edited 21 mins ago
Jamal♦
30.3k11119227
30.3k11119227
asked 4 hours ago
aria mossararia mossar
133
133
migrated from stackoverflow.com 24 mins ago
This question came from our site for professional and enthusiast programmers.
migrated from stackoverflow.com 24 mins ago
This question came from our site for professional and enthusiast programmers.
1
$begingroup$
Please read about SQL injection. Instead of building queries with string concatenation, use prepared statements with bound parameters. See this page and this post for some good examples.
$endgroup$
– John Conde
4 hours ago
$begingroup$
Do it all with one transaction instead of one transaction per row inserted.
$endgroup$
– Shawn
4 hours ago
add a comment |
1
$begingroup$
Please read about SQL injection. Instead of building queries with string concatenation, use prepared statements with bound parameters. See this page and this post for some good examples.
$endgroup$
– John Conde
4 hours ago
$begingroup$
Do it all with one transaction instead of one transaction per row inserted.
$endgroup$
– Shawn
4 hours ago
1
1
$begingroup$
Please read about SQL injection. Instead of building queries with string concatenation, use prepared statements with bound parameters. See this page and this post for some good examples.
$endgroup$
– John Conde
4 hours ago
$begingroup$
Please read about SQL injection. Instead of building queries with string concatenation, use prepared statements with bound parameters. See this page and this post for some good examples.
$endgroup$
– John Conde
4 hours ago
$begingroup$
Do it all with one transaction instead of one transaction per row inserted.
$endgroup$
– Shawn
4 hours ago
$begingroup$
Do it all with one transaction instead of one transaction per row inserted.
$endgroup$
– Shawn
4 hours ago
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
You are using a transaction per insert statement, that will basically do nothing as a single statement on it's own is effectively a transaction.
You want to perform the loop within a single transaction so that the disk is written just the once, rather than for every insert.
I'd suggest changing :-
for (int i = 0; i < itemArray.length(); i++) {
itemCode = itemArray.getJSONObject(i).getString("ITEMCODE");
itemBrand = itemArray.getJSONObject(i).getString("BRAND");
itemDesc = itemArray.getJSONObject(i).getString("DESCRIPTION");
DatabaseOperations dop = new DatabaseOperations(ctx);
dop.insertItems(dop, itemCode, itemBrand, itemDesc);
}
to :-
DatabaseOperations dop = new DatabaseOperations(ctx);
dop.getWritableDatabase().beginTransaction();
for (int i = 0; i < itemArray.length(); i++) {
itemCode = itemArray.getJSONObject(i).getString("ITEMCODE");
itemBrand = itemArray.getJSONObject(i).getString("BRAND");
itemDesc = itemArray.getJSONObject(i).getString("DESCRIPTION");
dop.insertItems(dop, itemCode, itemBrand, itemDesc);
}
dop.setTransactionSuccessful();
dop.endTransaction();
Along with changing the insertItems method to :-
public void insertItems (DatabaseOperations dop,
String itemCode, String brand, String desc) {
SQLiteDatabase sq = dop.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(TableData.TableInfo.COL_ITEMS_ITEMCODE, itemCode);
cv.put(TableData.TableInfo.COL_ITEMS_BRAND, brand);
cv.put(TableData.TableInfo.COL_ITEMS_DESCRIPTION, desc);
sq.insert(TableData.TableInfo.TB_ITEMS, null, cv);
}
Note this is in-principle code, it has not been run or tested and may therefore contain some errors.
Note that the comment in regard to SQL Injection does not take into consideration that the Android convenience methods, such as insert do provide protection against SQL injection as the values extracted from the ContentValues are appropriately enclosed when the underlying SQL is built by the methods.
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214379%2fsending-and-receiving-of-json-data-to-and-from-android-studio%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
You are using a transaction per insert statement, that will basically do nothing as a single statement on it's own is effectively a transaction.
You want to perform the loop within a single transaction so that the disk is written just the once, rather than for every insert.
I'd suggest changing :-
for (int i = 0; i < itemArray.length(); i++) {
itemCode = itemArray.getJSONObject(i).getString("ITEMCODE");
itemBrand = itemArray.getJSONObject(i).getString("BRAND");
itemDesc = itemArray.getJSONObject(i).getString("DESCRIPTION");
DatabaseOperations dop = new DatabaseOperations(ctx);
dop.insertItems(dop, itemCode, itemBrand, itemDesc);
}
to :-
DatabaseOperations dop = new DatabaseOperations(ctx);
dop.getWritableDatabase().beginTransaction();
for (int i = 0; i < itemArray.length(); i++) {
itemCode = itemArray.getJSONObject(i).getString("ITEMCODE");
itemBrand = itemArray.getJSONObject(i).getString("BRAND");
itemDesc = itemArray.getJSONObject(i).getString("DESCRIPTION");
dop.insertItems(dop, itemCode, itemBrand, itemDesc);
}
dop.setTransactionSuccessful();
dop.endTransaction();
Along with changing the insertItems method to :-
public void insertItems (DatabaseOperations dop,
String itemCode, String brand, String desc) {
SQLiteDatabase sq = dop.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(TableData.TableInfo.COL_ITEMS_ITEMCODE, itemCode);
cv.put(TableData.TableInfo.COL_ITEMS_BRAND, brand);
cv.put(TableData.TableInfo.COL_ITEMS_DESCRIPTION, desc);
sq.insert(TableData.TableInfo.TB_ITEMS, null, cv);
}
Note this is in-principle code, it has not been run or tested and may therefore contain some errors.
Note that the comment in regard to SQL Injection does not take into consideration that the Android convenience methods, such as insert do provide protection against SQL injection as the values extracted from the ContentValues are appropriately enclosed when the underlying SQL is built by the methods.
$endgroup$
add a comment |
$begingroup$
You are using a transaction per insert statement, that will basically do nothing as a single statement on it's own is effectively a transaction.
You want to perform the loop within a single transaction so that the disk is written just the once, rather than for every insert.
I'd suggest changing :-
for (int i = 0; i < itemArray.length(); i++) {
itemCode = itemArray.getJSONObject(i).getString("ITEMCODE");
itemBrand = itemArray.getJSONObject(i).getString("BRAND");
itemDesc = itemArray.getJSONObject(i).getString("DESCRIPTION");
DatabaseOperations dop = new DatabaseOperations(ctx);
dop.insertItems(dop, itemCode, itemBrand, itemDesc);
}
to :-
DatabaseOperations dop = new DatabaseOperations(ctx);
dop.getWritableDatabase().beginTransaction();
for (int i = 0; i < itemArray.length(); i++) {
itemCode = itemArray.getJSONObject(i).getString("ITEMCODE");
itemBrand = itemArray.getJSONObject(i).getString("BRAND");
itemDesc = itemArray.getJSONObject(i).getString("DESCRIPTION");
dop.insertItems(dop, itemCode, itemBrand, itemDesc);
}
dop.setTransactionSuccessful();
dop.endTransaction();
Along with changing the insertItems method to :-
public void insertItems (DatabaseOperations dop,
String itemCode, String brand, String desc) {
SQLiteDatabase sq = dop.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(TableData.TableInfo.COL_ITEMS_ITEMCODE, itemCode);
cv.put(TableData.TableInfo.COL_ITEMS_BRAND, brand);
cv.put(TableData.TableInfo.COL_ITEMS_DESCRIPTION, desc);
sq.insert(TableData.TableInfo.TB_ITEMS, null, cv);
}
Note this is in-principle code, it has not been run or tested and may therefore contain some errors.
Note that the comment in regard to SQL Injection does not take into consideration that the Android convenience methods, such as insert do provide protection against SQL injection as the values extracted from the ContentValues are appropriately enclosed when the underlying SQL is built by the methods.
$endgroup$
add a comment |
$begingroup$
You are using a transaction per insert statement, that will basically do nothing as a single statement on it's own is effectively a transaction.
You want to perform the loop within a single transaction so that the disk is written just the once, rather than for every insert.
I'd suggest changing :-
for (int i = 0; i < itemArray.length(); i++) {
itemCode = itemArray.getJSONObject(i).getString("ITEMCODE");
itemBrand = itemArray.getJSONObject(i).getString("BRAND");
itemDesc = itemArray.getJSONObject(i).getString("DESCRIPTION");
DatabaseOperations dop = new DatabaseOperations(ctx);
dop.insertItems(dop, itemCode, itemBrand, itemDesc);
}
to :-
DatabaseOperations dop = new DatabaseOperations(ctx);
dop.getWritableDatabase().beginTransaction();
for (int i = 0; i < itemArray.length(); i++) {
itemCode = itemArray.getJSONObject(i).getString("ITEMCODE");
itemBrand = itemArray.getJSONObject(i).getString("BRAND");
itemDesc = itemArray.getJSONObject(i).getString("DESCRIPTION");
dop.insertItems(dop, itemCode, itemBrand, itemDesc);
}
dop.setTransactionSuccessful();
dop.endTransaction();
Along with changing the insertItems method to :-
public void insertItems (DatabaseOperations dop,
String itemCode, String brand, String desc) {
SQLiteDatabase sq = dop.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(TableData.TableInfo.COL_ITEMS_ITEMCODE, itemCode);
cv.put(TableData.TableInfo.COL_ITEMS_BRAND, brand);
cv.put(TableData.TableInfo.COL_ITEMS_DESCRIPTION, desc);
sq.insert(TableData.TableInfo.TB_ITEMS, null, cv);
}
Note this is in-principle code, it has not been run or tested and may therefore contain some errors.
Note that the comment in regard to SQL Injection does not take into consideration that the Android convenience methods, such as insert do provide protection against SQL injection as the values extracted from the ContentValues are appropriately enclosed when the underlying SQL is built by the methods.
$endgroup$
You are using a transaction per insert statement, that will basically do nothing as a single statement on it's own is effectively a transaction.
You want to perform the loop within a single transaction so that the disk is written just the once, rather than for every insert.
I'd suggest changing :-
for (int i = 0; i < itemArray.length(); i++) {
itemCode = itemArray.getJSONObject(i).getString("ITEMCODE");
itemBrand = itemArray.getJSONObject(i).getString("BRAND");
itemDesc = itemArray.getJSONObject(i).getString("DESCRIPTION");
DatabaseOperations dop = new DatabaseOperations(ctx);
dop.insertItems(dop, itemCode, itemBrand, itemDesc);
}
to :-
DatabaseOperations dop = new DatabaseOperations(ctx);
dop.getWritableDatabase().beginTransaction();
for (int i = 0; i < itemArray.length(); i++) {
itemCode = itemArray.getJSONObject(i).getString("ITEMCODE");
itemBrand = itemArray.getJSONObject(i).getString("BRAND");
itemDesc = itemArray.getJSONObject(i).getString("DESCRIPTION");
dop.insertItems(dop, itemCode, itemBrand, itemDesc);
}
dop.setTransactionSuccessful();
dop.endTransaction();
Along with changing the insertItems method to :-
public void insertItems (DatabaseOperations dop,
String itemCode, String brand, String desc) {
SQLiteDatabase sq = dop.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(TableData.TableInfo.COL_ITEMS_ITEMCODE, itemCode);
cv.put(TableData.TableInfo.COL_ITEMS_BRAND, brand);
cv.put(TableData.TableInfo.COL_ITEMS_DESCRIPTION, desc);
sq.insert(TableData.TableInfo.TB_ITEMS, null, cv);
}
Note this is in-principle code, it has not been run or tested and may therefore contain some errors.
Note that the comment in regard to SQL Injection does not take into consideration that the Android convenience methods, such as insert do provide protection against SQL injection as the values extracted from the ContentValues are appropriately enclosed when the underlying SQL is built by the methods.
answered 3 hours ago
MikeT
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214379%2fsending-and-receiving-of-json-data-to-and-from-android-studio%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
$begingroup$
Please read about SQL injection. Instead of building queries with string concatenation, use prepared statements with bound parameters. See this page and this post for some good examples.
$endgroup$
– John Conde
4 hours ago
$begingroup$
Do it all with one transaction instead of one transaction per row inserted.
$endgroup$
– Shawn
4 hours ago