虽然在我的平板电脑上没有问题,但在我的模拟器上运行 json 解析大文件的速度非常慢。
我读到 JACKSON JSON 是一种非常快速的处理方法,所以我安装了它,但它更糟?
我做错了什么吗,我只是在使用核心库,所以几乎没有开销。
我将数据放在 sqllite 数据库中。它运行速度一样慢,也没有转储到 sqlite 中,因此 db 调用不是问题。
关于这是否尽可能优化的任何想法?
这是整个代码:
public boolean dowloadfilejson(double olng,double olat, double odistance,DatabaseHelper dbc)
{ URL url;
SQLiteDatabase db = dbc.getWritableDatabase();
db.setLockingEnabled(true);
db.execSQL("delete from deals_categories;delete from deals;");
db.beginTransaction();
//JsonFactory fac = Util.getJsonFactory(); // recycle JsonFactory
JsonFactory fac = new JsonFactory(); // or just use once.
JsonParser jp = null;
try {
url = new URL(URL + "lat=" + "41.778049" +"&lon="+ "-88.086898" + "&distance=10");
jp = fac.createJsonParser(url);
jp.nextValue();
while (jp.nextValue() != null)
{
//jp.getCurrentName();
if(jp.getCurrentName() =="lat")
{
Log.i("1",jp.getText());
}
if(jp.getCurrentName() =="lon")
{
Log.i("1",jp.getText());
}
if(jp.getCurrentName() =="distance")
{
Log.i("1",jp.getText());
}
if(jp.getCurrentName() =="totalSize")
{
Log.i("1",jp.getText());
}
if(jp.getCurrentName() =="items")
{
while(jp.nextValue() != JsonToken.END_ARRAY) //items array
{String id = "";
ContentValues cv = new ContentValues();
//Log.i("ITEM",jp.getText());
while(jp.nextValue() != JsonToken.END_OBJECT) //each item
{ //Log.i("ITEMin",jp.getText());
/* cv.put("deal_id",c.getString("_id"));
cv.put("mtype",c.getString("type"));
cv.put("dealUrl",c.getString("dealUrl"));
cv.put("options_option_buyUrl",c.getString("buyUrl"));
cv.put("options_option_redemptionLocations_redemptionLocation_city", c.getString("city"));
cv.put("startAt" ,c.getString("startAt"));
cv.put("endAt" ,c.getString("endAt"));
cv.put("lat" ,c.getString("lat"));
cv.put("lng" ,c.getString("lng"));
cv.put("mediumImageUrl" ,c.getString("mediumImageUrl"));
cv.put("announcementTitle" ,c.getString("title"));
*/
if(jp.getCurrentName() == "_id" )
{
id = jp.getText();
cv.put("deal_id",id);
}
if(jp.getCurrentName() == "type" )
{
cv.put("mtype",jp.getText());
}
if(jp.getCurrentName() == "dealUrl" )
{
cv.put("dealUrl",jp.getText());
}
if(jp.getCurrentName() == "buyUrl" )
{
cv.put("options_option_buyUrl",jp.getText());
}
if(jp.getCurrentName() == "city" )
{
cv.put("options_option_redemptionLocations_redemptionLocation_city",jp.getText());
}
if(jp.getCurrentName() == "startAt" )
{
cv.put("startAt",jp.getText());
}
if(jp.getCurrentName() == "endAt" )
{
cv.put("endAt",jp.getText());
}
if(jp.getCurrentName() == "lat" )
{
cv.put("lat",jp.getText());
}
if(jp.getCurrentName() == "lng" )
{
cv.put("lng",jp.getText());
}
if(jp.getCurrentName() == "mediumImageUrl" )
{
cv.put("mediumImageUrl" ,jp.getText());
}
if(jp.getCurrentName() == "title" )
{
cv.put("announcementTitle" ,jp.getText());
}
if(jp.getCurrentName() == "catname" )
{
String[] s = jp.getText().split("\\|");
for (String val : s) {
ContentValues cvcat = new ContentValues();
cvcat.put( "active" ,1);
cvcat.put("catname" ,val);
db.insertWithOnConflict ("categories", null, cvcat, SQLiteDatabase.CONFLICT_IGNORE);
cvcat = null;
ContentValues cvcatd = new ContentValues();
cvcatd.put("catname" ,val);
cvcatd.put("deal_id",id);
db.insertWithOnConflict ("deals_categories", null, cvcatd, SQLiteDatabase.CONFLICT_IGNORE);
cvcatd = null;
}
}
}
id = "";
db.insertWithOnConflict ("deals", null, cv, SQLiteDatabase.CONFLICT_IGNORE);
cv = null;
}
}
}
return true;
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
finally{
db.setTransactionSuccessful();
db.endTransaction();
/* if(ih!=null)
ihdc.close();
ihc.close();
ih.close();
*/
db.setLockingEnabled(false);
db.close();
dbc.close();
}
}
编辑
我发现处理速度非常慢的问题是 CONTENTVALUES。我想我会用那个 claass 报废,然后用手写的,看看是否有帮助。