我正在获取飞溅数据。我已经从服务器获取了将近7k的数据。从服务器获取数据时,我将其保存在本地数据库中,但问题是过程太慢。大约需要5分钟。我想解决问题。请帮忙。
从服务器获取数据并将其保存到数据库的代码:
private void productsDetailsApi() {
String tag_json_obj = "json_obj_req";
String url = Constants.PRODUCTS_DETAILS_URL;
pBar.setVisibility(View.VISIBLE);
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
url, new JSONObject(),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.e("product_response", response.toString());
try {
JSONArray jsonArray = response.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
JSONObject company = jsonObject.getJSONObject("company");
ModelProductDetail modelProductDetail = new ModelProductDetail();
modelProductDetail.setCompany_id(jsonObject.getString("company_id"));
modelProductDetail.setProduct_name_nl(jsonObject.getString("name_nl"));
modelProductDetail.setProduct_name_fr(jsonObject.getString("name_fr"));
modelProductDetail.setProduct_desc(jsonObject.getString("description"));
modelProductDetail.setProduct_id(jsonObject.getString("id"));
modelProductDetail.setEan_code(jsonObject.getString("ean_code").trim());
modelProductDetail.setArticle_code(jsonObject.getString("article_code").trim());
modelProductDetail.setProduct_mbh(jsonObject.getString("mbh"));
modelProductDetail.setProduct_msrp(jsonObject.getString("msrp"));
modelProductDetail.setProduct_source(jsonObject.getString("source"));
modelProductDetail.setCompany_name(company.getString("name"));
modelProductDetail.setFranco_trading_value("");
modelProductDetail.setFranco_product_value(company.getString("franco_amount_product"));
dbHelper.addProductsDetails(modelProductDetail);
}
dbHelper.close();
ArrayList<ModelProductDetail> modelProductCodeList = dbHelper.getProductsArticleCode();
Log.e("TAG", "ModelProductDetail:art " + modelProductCodeList.size());
shopDetailsApi();
} catch (Exception e) {
e.printStackTrace();
}
//pBar.setVisibility(View.GONE);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error: " + error.getMessage());
// pBar.setVisibility(View.GONE);
}
});
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(
100000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
}
数据库插入查询
//add products data
public void addProductsDetails(ModelProductDetail modelProductDetail) {
SQLiteDatabase productsDb = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_PRODUCT_ID, modelProductDetail.getProduct_id());
values.put(KEY_PRODUCT_NAME_FR, modelProductDetail.getProduct_name_fr());
values.put(KEY_PRODUCT_NAME_NL, modelProductDetail.getProduct_name_nl());
values.put(KEY_PRODUCT_DESC, modelProductDetail.getProduct_desc());
values.put(KEY_PRODUCT_ART, modelProductDetail.getArticle_code());
values.put(KEY_PRODUCT_EAN, modelProductDetail.getEan_code());
values.put(KEY_PRODUCT_MBH, modelProductDetail.getProduct_mbh());
values.put(KEY_PRODUCT_MSRP, modelProductDetail.getProduct_msrp());
values.put(KEY_PRODUCT_SOURCE, modelProductDetail.getProduct_source());
values.put(KEY_COMPANY_ID, modelProductDetail.getCompany_id());
values.put(KEY_COMPANY_NAME, modelProductDetail.getCompany_name());
values.put(KEY_FRANCO_TRADING, modelProductDetail.getFranco_trading_value());
values.put(KEY_FRANCO_PRODUCT, modelProductDetail.getFranco_product_value());
productsDb.insert(TABLE_PRODUCT_DETAILS, null, values);
productsDb.close();
}
答案 0 :(得分:1)
您可以尝试将所有插入内容包装在交易中,例如:-
JSONArray jsonArray = response.getJSONArray("data");
dbHelper.getWritableDatabase.beginTransaction(); //<<<<<<<<<< ADDED
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
JSONObject company = jsonObject.getJSONObject("company");
ModelProductDetail modelProductDetail = new ModelProductDetail();
modelProductDetail.setCompany_id(jsonObject.getString("company_id"));
modelProductDetail.setProduct_name_nl(jsonObject.getString("name_nl"));
modelProductDetail.setProduct_name_fr(jsonObject.getString("name_fr"));
modelProductDetail.setProduct_desc(jsonObject.getString("description"));
modelProductDetail.setProduct_id(jsonObject.getString("id"));
modelProductDetail.setEan_code(jsonObject.getString("ean_code").trim());
modelProductDetail.setArticle_code(jsonObject.getString("article_code").trim());
modelProductDetail.setProduct_mbh(jsonObject.getString("mbh"));
modelProductDetail.setProduct_msrp(jsonObject.getString("msrp"));
modelProductDetail.setProduct_source(jsonObject.getString("source"));
modelProductDetail.setCompany_name(company.getString("name"));
modelProductDetail.setFranco_trading_value("");
modelProductDetail.setFranco_product_value(company.getString("franco_amount_product"));
dbHelper.addProductsDetails(modelProductDetail);
}
dbHelper.getWritableDatabase.setTransactionSuccessful(); //<<<<<<<<<< ADDED
dbHelper.getWritableDatabase.endTransaction(); //<<<<<<<<<< ADDED
dbHelper.close();
要使用上述内容,您还必须删除行
productsDb.close();
通过插入查询