com.lzy.okgo.utils.OkLogger Java Examples

The following examples show how to use com.lzy.okgo.utils.OkLogger. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DBUtils.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
/**
 * SQLite数据库中一个特殊的名叫 SQLITE_MASTER 上执行一个SELECT查询以获得所有表的索引。每一个 SQLite 数据库都有一个叫 SQLITE_MASTER 的表, 它定义数据库的模式。
 * SQLITE_MASTER 表看起来如下:
 * CREATE TABLE sqlite_master (
 * type TEXT,
 * name TEXT,
 * tbl_name TEXT,
 * rootpage INTEGER,
 * sql TEXT
 * );
 * 对于表来说,type 字段永远是 ‘table’,name 字段永远是表的名字。
 */
public static boolean isTableExists(SQLiteDatabase db, String tableName) {
    if (tableName == null || db == null || !db.isOpen()) return false;

    Cursor cursor = null;
    int count = 0;
    try {
        cursor = db.rawQuery("SELECT COUNT(*) FROM sqlite_master WHERE type = ? AND name = ?", new String[]{"table", tableName});
        if (!cursor.moveToFirst()) {
            return false;
        }
        count = cursor.getInt(0);
    } catch (Exception e) {
        OkLogger.printStackTrace(e);
    } finally {
        if (cursor != null) cursor.close();
    }
    return count > 0;
}
 
Example #2
Source File: BaseDao.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
/** 按条件查询对象并返回集合 */
public List<T> query(String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) {
    long start = System.currentTimeMillis();
    lock.lock();
    List<T> list = new ArrayList<>();
    Cursor cursor = null;
    try {
        database.beginTransaction();
        cursor = database.query(getTableName(), columns, selection, selectionArgs, groupBy, having, orderBy, limit);
        while (!cursor.isClosed() && cursor.moveToNext()) {
            list.add(parseCursorToBean(cursor));
        }
        database.setTransactionSuccessful();
    } catch (Exception e) {
        OkLogger.printStackTrace(e);
    } finally {
        closeDatabase(null, cursor);
        database.endTransaction();
        lock.unlock();
        OkLogger.v(TAG, System.currentTimeMillis() - start + " query");
    }
    return list;
}
 
Example #3
Source File: DBUtils.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
public static boolean isFieldExists(SQLiteDatabase db, String tableName, String fieldName) {
    if (tableName == null || db == null || fieldName == null || !db.isOpen()) return false;

    Cursor cursor = null;
    try {
        cursor = db.rawQuery("SELECT * FROM " + tableName + " LIMIT 0", null);
        return cursor != null && cursor.getColumnIndex(fieldName) != -1;
    } catch (Exception e) {
        OkLogger.printStackTrace(e);
        return false;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}
 
Example #4
Source File: BaseDao.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
/** 按条件查询对象并返回集合 */
public List<T> query(SQLiteDatabase database, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) {
    List<T> list = new ArrayList<>();
    Cursor cursor = null;
    try {
        cursor = database.query(getTableName(), columns, selection, selectionArgs, groupBy, having, orderBy, limit);
        while (!cursor.isClosed() && cursor.moveToNext()) {
            list.add(parseCursorToBean(cursor));
        }
    } catch (Exception e) {
        OkLogger.printStackTrace(e);
    } finally {
        closeDatabase(null, cursor);
    }
    return list;
}
 
Example #5
Source File: BaseDao.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
/** 更新一条记录 */
public boolean update(ContentValues contentValues, String whereClause, String[] whereArgs) {
    long start = System.currentTimeMillis();
    lock.lock();
    try {
        database.beginTransaction();
        database.update(getTableName(), contentValues, whereClause, whereArgs);
        database.setTransactionSuccessful();
        return true;
    } catch (Exception e) {
        OkLogger.printStackTrace(e);
    } finally {
        database.endTransaction();
        lock.unlock();
        OkLogger.v(TAG, System.currentTimeMillis() - start + " updateContentValues");
    }
    return false;
}
 
Example #6
Source File: BaseDao.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
/** 更新一条记录 */
public boolean update(T t, String whereClause, String[] whereArgs) {
    if (t == null) return false;
    long start = System.currentTimeMillis();
    lock.lock();
    try {
        database.beginTransaction();
        database.update(getTableName(), getContentValues(t), whereClause, whereArgs);
        database.setTransactionSuccessful();
        return true;
    } catch (Exception e) {
        OkLogger.printStackTrace(e);
    } finally {
        database.endTransaction();
        lock.unlock();
        OkLogger.v(TAG, System.currentTimeMillis() - start + " updateT");
    }
    return false;
}
 
Example #7
Source File: BaseDao.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
/** 插入一条记录 */
public boolean insert(T t) {
    if (t == null) return false;
    long start = System.currentTimeMillis();
    lock.lock();
    try {
        database.beginTransaction();
        database.insert(getTableName(), null, getContentValues(t));
        database.setTransactionSuccessful();
        return true;
    } catch (Exception e) {
        OkLogger.printStackTrace(e);
    } finally {
        database.endTransaction();
        lock.unlock();
        OkLogger.v(TAG, System.currentTimeMillis() - start + " insertT");
    }
    return false;
}
 
Example #8
Source File: BaseDao.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
public boolean replace(List<T> ts) {
    if (ts == null) return false;
    long start = System.currentTimeMillis();
    lock.lock();
    try {
        database.beginTransaction();
        for (T t : ts) {
            database.replace(getTableName(), null, getContentValues(t));
        }
        database.setTransactionSuccessful();
        return true;
    } catch (Exception e) {
        OkLogger.printStackTrace(e);
    } finally {
        database.endTransaction();
        lock.unlock();
        OkLogger.v(TAG, System.currentTimeMillis() - start + " replaceList");
    }
    return false;
}
 
Example #9
Source File: BaseDao.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
public boolean replace(ContentValues contentValues) {
    long start = System.currentTimeMillis();
    lock.lock();
    try {
        database.beginTransaction();
        database.replace(getTableName(), null, contentValues);
        database.setTransactionSuccessful();
        return true;
    } catch (Exception e) {
        OkLogger.printStackTrace(e);
    } finally {
        database.endTransaction();
        lock.unlock();
        OkLogger.v(TAG, System.currentTimeMillis() - start + " replaceContentValues");
    }
    return false;
}
 
Example #10
Source File: BaseDao.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
/**
 * replace 语句有如下行为特点
 * 1. replace语句会删除原有的一条记录, 并且插入一条新的记录来替换原记录。
 * 2. 一般用replace语句替换一条记录的所有列, 如果在replace语句中没有指定某列, 在replace之后这列的值被置空 。
 * 3. replace语句根据主键的值确定被替换的是哪一条记录
 * 4. 如果执行replace语句时, 不存在要替换的记录, 那么就会插入一条新的记录。
 * 5. replace语句不能根据where子句来定位要被替换的记录
 * 6. 如果新插入的或替换的记录中, 有字段和表中的其他记录冲突, 那么会删除那条其他记录。
 */
public boolean replace(T t) {
    if (t == null) return false;
    long start = System.currentTimeMillis();
    lock.lock();
    try {
        database.beginTransaction();
        database.replace(getTableName(), null, getContentValues(t));
        database.setTransactionSuccessful();
        return true;
    } catch (Exception e) {
        OkLogger.printStackTrace(e);
    } finally {
        database.endTransaction();
        lock.unlock();
        OkLogger.v(TAG, System.currentTimeMillis() - start + " replaceT");
    }
    return false;
}
 
Example #11
Source File: BaseDao.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
public boolean deleteList(List<Pair<String, String[]>> where) {
    long start = System.currentTimeMillis();
    lock.lock();
    try {
        database.beginTransaction();
        for (Pair<String, String[]> pair : where) {
            database.delete(getTableName(), pair.first, pair.second);
        }
        database.setTransactionSuccessful();
        return true;
    } catch (Exception e) {
        OkLogger.printStackTrace(e);
    } finally {
        database.endTransaction();
        lock.unlock();
        OkLogger.v(TAG, System.currentTimeMillis() - start + " deleteList");
    }
    return false;
}
 
Example #12
Source File: BaseDao.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
/** 根据条件删除数据库中的数据 */
public boolean delete(String whereClause, String[] whereArgs) {
    long start = System.currentTimeMillis();
    lock.lock();
    try {
        database.beginTransaction();
        database.delete(getTableName(), whereClause, whereArgs);
        database.setTransactionSuccessful();
        return true;
    } catch (Exception e) {
        OkLogger.printStackTrace(e);
    } finally {
        database.endTransaction();
        lock.unlock();
        OkLogger.v(TAG, System.currentTimeMillis() - start + " delete");
    }
    return false;
}
 
Example #13
Source File: BaseDao.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
/** 插入多条记录 */
public boolean insert(List<T> ts) {
    if (ts == null) return false;
    long start = System.currentTimeMillis();
    lock.lock();
    try {
        database.beginTransaction();
        for (T t : ts) {
            database.insert(getTableName(), null, getContentValues(t));
        }
        database.setTransactionSuccessful();
        return true;
    } catch (Exception e) {
        OkLogger.printStackTrace(e);
    } finally {
        database.endTransaction();
        lock.unlock();
        OkLogger.v(TAG, System.currentTimeMillis() - start + " insertList");
    }
    return false;
}
 
Example #14
Source File: BaseDao.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
/** 插入多条记录 */
public boolean insert(List<T> ts) {
    if (ts == null) return false;
    long start = System.currentTimeMillis();
    lock.lock();
    try {
        database.beginTransaction();
        for (T t : ts) {
            database.insert(getTableName(), null, getContentValues(t));
        }
        database.setTransactionSuccessful();
        return true;
    } catch (Exception e) {
        OkLogger.printStackTrace(e);
    } finally {
        database.endTransaction();
        lock.unlock();
        OkLogger.v(TAG, System.currentTimeMillis() - start + " insertList");
    }
    return false;
}
 
Example #15
Source File: BaseDao.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
/** 插入一条记录 */
public boolean insert(T t) {
    if (t == null) return false;
    long start = System.currentTimeMillis();
    lock.lock();
    try {
        database.beginTransaction();
        database.insert(getTableName(), null, getContentValues(t));
        database.setTransactionSuccessful();
        return true;
    } catch (Exception e) {
        OkLogger.printStackTrace(e);
    } finally {
        database.endTransaction();
        lock.unlock();
        OkLogger.v(TAG, System.currentTimeMillis() - start + " insertT");
    }
    return false;
}
 
Example #16
Source File: DBUtils.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
public static boolean isFieldExists(SQLiteDatabase db, String tableName, String fieldName) {
    if (tableName == null || db == null || fieldName == null || !db.isOpen()) return false;

    Cursor cursor = null;
    try {
        cursor = db.rawQuery("SELECT * FROM " + tableName + " LIMIT 0", null);
        return cursor != null && cursor.getColumnIndex(fieldName) != -1;
    } catch (Exception e) {
        OkLogger.printStackTrace(e);
        return false;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}
 
Example #17
Source File: DBUtils.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
/**
 * SQLite数据库中一个特殊的名叫 SQLITE_MASTER 上执行一个SELECT查询以获得所有表的索引。每一个 SQLite 数据库都有一个叫 SQLITE_MASTER 的表, 它定义数据库的模式。
 * SQLITE_MASTER 表看起来如下:
 * CREATE TABLE sqlite_master (
 * type TEXT,
 * name TEXT,
 * tbl_name TEXT,
 * rootpage INTEGER,
 * sql TEXT
 * );
 * 对于表来说,type 字段永远是 ‘table’,name 字段永远是表的名字。
 */
public static boolean isTableExists(SQLiteDatabase db, String tableName) {
    if (tableName == null || db == null || !db.isOpen()) return false;

    Cursor cursor = null;
    int count = 0;
    try {
        cursor = db.rawQuery("SELECT COUNT(*) FROM sqlite_master WHERE type = ? AND name = ?", new String[]{"table", tableName});
        if (!cursor.moveToFirst()) {
            return false;
        }
        count = cursor.getInt(0);
    } catch (Exception e) {
        OkLogger.printStackTrace(e);
    } finally {
        if (cursor != null) cursor.close();
    }
    return count > 0;
}
 
Example #18
Source File: BaseDao.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
/** 根据条件删除数据库中的数据 */
public boolean delete(String whereClause, String[] whereArgs) {
    long start = System.currentTimeMillis();
    lock.lock();
    try {
        database.beginTransaction();
        database.delete(getTableName(), whereClause, whereArgs);
        database.setTransactionSuccessful();
        return true;
    } catch (Exception e) {
        OkLogger.printStackTrace(e);
    } finally {
        database.endTransaction();
        lock.unlock();
        OkLogger.v(TAG, System.currentTimeMillis() - start + " delete");
    }
    return false;
}
 
Example #19
Source File: BaseDao.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
public boolean deleteList(List<Pair<String, String[]>> where) {
    long start = System.currentTimeMillis();
    lock.lock();
    try {
        database.beginTransaction();
        for (Pair<String, String[]> pair : where) {
            database.delete(getTableName(), pair.first, pair.second);
        }
        database.setTransactionSuccessful();
        return true;
    } catch (Exception e) {
        OkLogger.printStackTrace(e);
    } finally {
        database.endTransaction();
        lock.unlock();
        OkLogger.v(TAG, System.currentTimeMillis() - start + " deleteList");
    }
    return false;
}
 
Example #20
Source File: BaseDao.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
/**
 * replace 语句有如下行为特点
 * 1. replace语句会删除原有的一条记录, 并且插入一条新的记录来替换原记录。
 * 2. 一般用replace语句替换一条记录的所有列, 如果在replace语句中没有指定某列, 在replace之后这列的值被置空 。
 * 3. replace语句根据主键的值确定被替换的是哪一条记录
 * 4. 如果执行replace语句时, 不存在要替换的记录, 那么就会插入一条新的记录。
 * 5. replace语句不能根据where子句来定位要被替换的记录
 * 6. 如果新插入的或替换的记录中, 有字段和表中的其他记录冲突, 那么会删除那条其他记录。
 */
public boolean replace(T t) {
    if (t == null) return false;
    long start = System.currentTimeMillis();
    lock.lock();
    try {
        database.beginTransaction();
        database.replace(getTableName(), null, getContentValues(t));
        database.setTransactionSuccessful();
        return true;
    } catch (Exception e) {
        OkLogger.printStackTrace(e);
    } finally {
        database.endTransaction();
        lock.unlock();
        OkLogger.v(TAG, System.currentTimeMillis() - start + " replaceT");
    }
    return false;
}
 
Example #21
Source File: BaseDao.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
public boolean replace(ContentValues contentValues) {
    long start = System.currentTimeMillis();
    lock.lock();
    try {
        database.beginTransaction();
        database.replace(getTableName(), null, contentValues);
        database.setTransactionSuccessful();
        return true;
    } catch (Exception e) {
        OkLogger.printStackTrace(e);
    } finally {
        database.endTransaction();
        lock.unlock();
        OkLogger.v(TAG, System.currentTimeMillis() - start + " replaceContentValues");
    }
    return false;
}
 
Example #22
Source File: BaseDao.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
public boolean replace(List<T> ts) {
    if (ts == null) return false;
    long start = System.currentTimeMillis();
    lock.lock();
    try {
        database.beginTransaction();
        for (T t : ts) {
            database.replace(getTableName(), null, getContentValues(t));
        }
        database.setTransactionSuccessful();
        return true;
    } catch (Exception e) {
        OkLogger.printStackTrace(e);
    } finally {
        database.endTransaction();
        lock.unlock();
        OkLogger.v(TAG, System.currentTimeMillis() - start + " replaceList");
    }
    return false;
}
 
Example #23
Source File: BaseDao.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
/** 更新一条记录 */
public boolean update(T t, String whereClause, String[] whereArgs) {
    if (t == null) return false;
    long start = System.currentTimeMillis();
    lock.lock();
    try {
        database.beginTransaction();
        database.update(getTableName(), getContentValues(t), whereClause, whereArgs);
        database.setTransactionSuccessful();
        return true;
    } catch (Exception e) {
        OkLogger.printStackTrace(e);
    } finally {
        database.endTransaction();
        lock.unlock();
        OkLogger.v(TAG, System.currentTimeMillis() - start + " updateT");
    }
    return false;
}
 
Example #24
Source File: BaseDao.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
/** 更新一条记录 */
public boolean update(ContentValues contentValues, String whereClause, String[] whereArgs) {
    long start = System.currentTimeMillis();
    lock.lock();
    try {
        database.beginTransaction();
        database.update(getTableName(), contentValues, whereClause, whereArgs);
        database.setTransactionSuccessful();
        return true;
    } catch (Exception e) {
        OkLogger.printStackTrace(e);
    } finally {
        database.endTransaction();
        lock.unlock();
        OkLogger.v(TAG, System.currentTimeMillis() - start + " updateContentValues");
    }
    return false;
}
 
Example #25
Source File: BaseDao.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
/** 按条件查询对象并返回集合 */
public List<T> query(SQLiteDatabase database, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) {
    List<T> list = new ArrayList<>();
    Cursor cursor = null;
    try {
        cursor = database.query(getTableName(), columns, selection, selectionArgs, groupBy, having, orderBy, limit);
        while (!cursor.isClosed() && cursor.moveToNext()) {
            list.add(parseCursorToBean(cursor));
        }
    } catch (Exception e) {
        OkLogger.printStackTrace(e);
    } finally {
        closeDatabase(null, cursor);
    }
    return list;
}
 
Example #26
Source File: DownloadTask.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
public void start() {
    if (OkDownload.getInstance().getTask(progress.tag) == null || DownloadManager.getInstance().get(progress.tag) == null) {
        throw new IllegalStateException("you must call DownloadTask#save() before DownloadTask#start()!");
    }
    if (progress.status == Progress.NONE || progress.status == Progress.PAUSE || progress.status == Progress.ERROR) {
        postOnStart(progress);
        postWaiting(progress);
        priorityRunnable = new PriorityRunnable(progress.priority, this);
        executor.execute(priorityRunnable);
    } else if (progress.status == Progress.FINISH) {
        if (progress.filePath == null) {
            postOnError(progress, new StorageException("the file of the task with tag:" + progress.tag + " may be invalid or damaged, please call the method restart() to download again!"));
        } else {
            File file = new File(progress.filePath);
            if (file.exists() && file.length() == progress.totalSize) {
                postOnFinish(progress, new File(progress.filePath));
            } else {
                postOnError(progress, new StorageException("the file " + progress.filePath + " may be invalid or damaged, please call the method restart() to download again!"));
            }
        }
    } else {
        OkLogger.w("the task with tag " + progress.tag + " is already in the download queue, current task status is " + progress.status);
    }
}
 
Example #27
Source File: BaseDao.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
/** 按条件查询对象并返回集合 */
public List<T> query(String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) {
    long start = System.currentTimeMillis();
    lock.lock();
    List<T> list = new ArrayList<>();
    Cursor cursor = null;
    try {
        database.beginTransaction();
        cursor = database.query(getTableName(), columns, selection, selectionArgs, groupBy, having, orderBy, limit);
        while (!cursor.isClosed() && cursor.moveToNext()) {
            list.add(parseCursorToBean(cursor));
        }
        database.setTransactionSuccessful();
    } catch (Exception e) {
        OkLogger.printStackTrace(e);
    } finally {
        closeDatabase(null, cursor);
        database.endTransaction();
        lock.unlock();
        OkLogger.v(TAG, System.currentTimeMillis() - start + " query");
    }
    return list;
}
 
Example #28
Source File: SerializableCookie.java    From BaseProject with Apache License 2.0 5 votes vote down vote up
public static byte[] cookieToBytes(String host, Cookie cookie) {
    SerializableCookie serializableCookie = new SerializableCookie(host, cookie);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try {
        ObjectOutputStream outputStream = new ObjectOutputStream(os);
        outputStream.writeObject(serializableCookie);
    } catch (IOException e) {
        OkLogger.printStackTrace(e);
        return null;
    }
    return os.toByteArray();
}
 
Example #29
Source File: HttpsUtils.java    From BaseProject with Apache License 2.0 5 votes vote down vote up
private static KeyManager[] prepareKeyManager(InputStream bksFile, String password) {
    try {
        if (bksFile == null || password == null) return null;
        KeyStore clientKeyStore = KeyStore.getInstance("BKS");
        clientKeyStore.load(bksFile, password.toCharArray());
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(clientKeyStore, password.toCharArray());
        return kmf.getKeyManagers();
    } catch (Exception e) {
        OkLogger.printStackTrace(e);
    }
    return null;
}
 
Example #30
Source File: HttpHeaders.java    From BaseProject with Apache License 2.0 5 votes vote down vote up
public final String toJSONString() {
    JSONObject jsonObject = new JSONObject();
    try {
        for (Map.Entry<String, String> entry : headersMap.entrySet()) {
            jsonObject.put(entry.getKey(), entry.getValue());
        }
    } catch (JSONException e) {
        OkLogger.printStackTrace(e);
    }
    return jsonObject.toString();
}