Skip to content

Commit

Permalink
升级1.7.1, 优化下载列表顺序问题,暴露部分方法
Browse files Browse the repository at this point in the history
  • Loading branch information
jeasonlzy committed Aug 10, 2016
1 parent e16358c commit 669fdb9
Show file tree
Hide file tree
Showing 20 changed files with 136 additions and 49 deletions.
23 changes: 10 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@

对于Eclipse不能运行项目的,提供了apk供直接运行

### 或者点击下载Demo [okhttputils_v1.7.0.apk](https://github.com/jeasonlzy0216/OkHttpUtils/blob/master/okhttputils_v1.7.0.apk?raw=true)
### 或者点击下载Demo [okhttputils_v1.7.1.apk](https://github.com/jeasonlzy0216/OkHttpUtils/blob/master/okhttputils_v1.7.1.apk?raw=true)

本项目Demo的网络请求是我自己的服务器,有时候可能不稳定,网速比较慢时请耐心等待。。

* 对于Android Studio的用户,可以选择添加:
```java
compile 'com.lzy.net:okhttputils:1.7.0' //可以单独使用,不需要依赖下方的扩展包
compile 'com.lzy.net:okhttpserver:1.0.0' //扩展了下载管理和上传管理,根据需要添加
compile 'com.lzy.net:okhttputils:1.7.1' //可以单独使用,不需要依赖下方的扩展包
compile 'com.lzy.net:okhttpserver:1.0.1' //扩展了下载管理和上传管理,根据需要添加

compile 'com.lzy.net:okhttputils:+' //版本号使用 + 可以自动引用最新版
compile 'com.lzy.net:okhttpserver:+' //版本号使用 + 可以自动引用最新版
Expand All @@ -48,8 +48,8 @@
```
* 对于Eclipse的用户,可以选择添加 `/lib` 目录下的:
```java
okhttputils-1.7.0.jar
okhttpserver-1.0.0.jar
okhttputils-1.7.1.jar
okhttpserver-1.0.1.jar
```

#### 其中的图片选择是我的另一个开源项目,完全仿微信的图片选择库,自带 矩形图片裁剪 和 圆形图片裁剪 功能,有需要的可以去下载使用,附上地址:[https://github.com/jeasonlzy0216/ImagePicker](https://github.com/jeasonlzy0216/ImagePicker)
Expand Down Expand Up @@ -163,14 +163,11 @@ callback一共有以下 7 个回调,除`onResponse`必须实现以外,其余均
* upProgress():上传进度的回调
* downloadProgress():下载进度的回调

> 无缓存模式,请求成功调用顺序(upProgress只在有请求体的情况下回调)
> onBefore -> upProgress -> parseNetworkResponse -> downloadProgress -> onResponse -> onAfter
> UI线程 UI线程 子线程 UI线程 UI线程 UI线程
>
> 无缓存模式,请求失败调用顺序
> onBefore -> onError -> onAfter
> UI线程 UI线程 UI线程
#### 无缓存模式,请求成功调用顺序(upProgress只在有请求体的情况下回调)
> onBefore(UI线程) -> upProgress(UI线程) -> parseNetworkResponse(子线程) -> downloadProgress(UI线程) -> onResponse(UI线程) -> onAfter(UI线程)
#### 无缓存模式,请求失败调用顺序
> onBefore(UI线程) -> onError(UI线程) -> onAfter(UI线程)
### 1.基本的网络请求
```java
Expand Down
9 changes: 5 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "com.lzy.okhttpdemo"
minSdkVersion 14
targetSdkVersion 22
versionCode 20
versionName "1.7.0"
versionCode 21
versionName "1.7.1"
}
buildTypes {
release {
Expand Down Expand Up @@ -42,8 +42,9 @@ dependencies {
compile 'com.google.code.gson:gson:2.5'
compile 'com.lzy.widget:imagepicker:0.3.2'
compile 'com.lzy.widget:view-core:0.2.1'
// compile 'com.lzy.net:okhttputils:+'
// compile 'com.lzy.net:okhttpserver:+'

// compile 'com.lzy.net:okhttputils:+'
// compile 'com.lzy.net:okhttpserver:+'
compile project(':okhttputils')
compile project(':okhttpserver')
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ public void run() {
});
return null;
}
}
}
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion okhttpserver/bintray.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

version = "1.0.0" // 数据仓库依赖第三部分
version = "1.0.1" // 数据仓库依赖第三部分

def siteUrl = 'https://github.com/jeasonlzy0216/OkHttpUtils'
def gitUrl = 'https://github.com/jeasonlzy0216/OkHttpUtils.git'
Expand Down
6 changes: 3 additions & 3 deletions okhttpserver/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 8
targetSdkVersion 22
versionCode 10
versionName "1.0.0"
versionCode 11
versionName "1.0.1"
}
buildTypes {
release {
Expand All @@ -20,7 +20,7 @@ android {

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
// compile 'com.lzy.net:okhttputils:+'
// compile 'com.lzy.net:okhttputils:1.7.1'
compile project(':okhttputils')
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ protected DownloadInfo doInBackground(Void... params) {
}

private void postMessage(String errorMsg, Exception e) {
DownloadDBManager.INSTANCE.replace(mDownloadInfo); //发消息前首先更新数据库
DownloadDBManager.INSTANCE.update(mDownloadInfo); //发消息前首先更新数据库
DownloadUIHandler.MessageBean messageBean = new DownloadUIHandler.MessageBean();
messageBean.downloadInfo = mDownloadInfo;
messageBean.errorMsg = errorMsg;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,26 @@ public void delete(String key) {
}
}

/** 创建下载任务 */
public void create(DownloadInfo entity) {
mLock.lock();
try {
infoDao.create(entity);
} finally {
mLock.unlock();
}
}

/** 更新下载任务 */
public void update(DownloadInfo entity) {
mLock.lock();
try {
infoDao.update(entity);
} finally {
mLock.unlock();
}
}

/** 清空下载任务 */
public boolean clear() {
mLock.lock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ public void delete(String taskKey) {
delete(DownloadInfo.TASK_KEY + "=?", new String[]{taskKey});
}

public int update(DownloadInfo downloadInfo) {
return update(downloadInfo, DownloadInfo.TASK_KEY + "=?", new String[]{downloadInfo.getTaskKey()});
}

@Override
public List<DownloadInfo> getAll() {
return get(null, null, null, null, null, DownloadInfo.ID + " ASC", null);
}

@Override
public DownloadInfo parseCursorToBean(Cursor cursor) {
return DownloadInfo.parseCursorToBean(cursor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.text.TextUtils;

import com.lzy.okhttputils.model.HttpHeaders;
import com.lzy.okhttputils.utils.OkLogger;

import java.io.File;
Expand Down Expand Up @@ -29,7 +30,7 @@ public static String getNetFileName(Response response, String url) {

/** 解析文件头 Content-Disposition:attachment;filename=FileName.txt */
private static String getHeaderFileName(Response response) {
String dispositionHeader = response.header("Content-Disposition");
String dispositionHeader = response.header(HttpHeaders.HEAD_KEY_CONTENT_DISPOSITION);
if (dispositionHeader != null) {
String split = "filename=";
int indexOf = dispositionHeader.indexOf(split);
Expand Down Expand Up @@ -64,4 +65,4 @@ public static boolean deleteFile(String path) {
}
return false;
}
}
}
2 changes: 1 addition & 1 deletion okhttputils/bintray.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

version = "1.7.0" // 数据仓库依赖第三部分
version = "1.7.1" // 数据仓库依赖第三部分

def siteUrl = 'https://github.com/jeasonlzy0216/OkHttpUtils'
def gitUrl = 'https://github.com/jeasonlzy0216/OkHttpUtils.git'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ protected final void closeDatabase(SQLiteDatabase database, Cursor cursor) {
protected abstract String getTableName();

/** 需要数据库中有个 _id 的字段 */
public final int count() {
public int count() {
return countColumn("_id");
}

/** 返回一列的总记录数量 */
public final int countColumn(String columnName) {
public int countColumn(String columnName) {
String sql = "SELECT COUNT(?) FROM " + getTableName();
SQLiteDatabase database = openReader();
Cursor cursor = null;
Expand All @@ -63,12 +63,12 @@ public final int countColumn(String columnName) {
}

/** 删除所有数据 */
public final int deleteAll() {
public int deleteAll() {
return delete(null, null);
}

/** 根据条件删除数据库中的数据 */
public final int delete(String whereClause, String[] whereArgs) {
public int delete(String whereClause, String[] whereArgs) {
SQLiteDatabase database = openWriter();
try {
database.beginTransaction();
Expand All @@ -85,17 +85,17 @@ public final int delete(String whereClause, String[] whereArgs) {
}

/** 查询并返回所有对象的集合 */
public final List<T> getAll() {
public List<T> getAll() {
return get(null, null);
}

/** 按条件查询对象并返回集合 */
public final List<T> get(String selection, String[] selectionArgs) {
public List<T> get(String selection, String[] selectionArgs) {
return get(null, selection, selectionArgs, null, null, null, null);
}

/** 按条件查询对象并返回集合 */
public final List<T> get(String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) {
public List<T> get(String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) {
SQLiteDatabase database = openReader();
List<T> list = new ArrayList<>();
Cursor cursor = null;
Expand All @@ -115,9 +115,6 @@ public final List<T> get(String[] columns, String selection, String[] selectionA
return list;
}

/** 将Cursor解析成对应的JavaBean */
public abstract T parseCursorToBean(Cursor cursor);

/**
* replace 语句有如下行为特点
* 1. replace语句会删除原有的一条记录, 并且插入一条新的记录来替换原记录。
Expand All @@ -129,11 +126,26 @@ public final List<T> get(String[] columns, String selection, String[] selectionA
*/
public long replace(T t) {
SQLiteDatabase database = openWriter();
ContentValues values = getContentValues(t);
try {
database.beginTransaction();
long id = database.replace(getTableName(), null, getContentValues(t));
database.setTransactionSuccessful();
return id;
} catch (Exception e) {
OkLogger.e(e);
} finally {
database.endTransaction();
closeDatabase(database, null);
}
return 0;
}

/** 创建一条记录 */
public long create(T t) {
SQLiteDatabase database = openWriter();
try {
database.beginTransaction();
long id = database.replace(getTableName(), null, values);
long id = database.insert(getTableName(), null, getContentValues(t));
database.setTransactionSuccessful();
return id;
} catch (Exception e) {
Expand All @@ -145,6 +157,26 @@ public long replace(T t) {
return 0;
}

/** 更新一条记录 */
public int update(T t, String whereClause, String[] whereArgs) {
SQLiteDatabase database = openWriter();
try {
database.beginTransaction();
int count = database.update(getTableName(), getContentValues(t), whereClause, whereArgs);
database.setTransactionSuccessful();
return count;
} catch (Exception e) {
OkLogger.e(e);
} finally {
database.endTransaction();
closeDatabase(database, null);
}
return 0;
}

/** 将Cursor解析成对应的JavaBean */
public abstract T parseCursorToBean(Cursor cursor);

/** 需要替换的列 */
public abstract ContentValues getContentValues(T t);
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class HttpHeaders implements Serializable {
public static final String HEAD_KEY_CONTENT_TYPE = "Content-Type";
public static final String HEAD_KEY_CONTENT_LENGTH = "Content-Length";
public static final String HEAD_KEY_CONTENT_ENCODING = "Content-Encoding";
public static final String HEAD_KEY_CONTENT_DISPOSITION = "Content-Disposition";
public static final String HEAD_KEY_CONTENT_RANGE = "Content-Range";
public static final String HEAD_KEY_CACHE_CONTROL = "Cache-Control";
public static final String HEAD_KEY_CONNECTION = "Connection";
Expand Down Expand Up @@ -101,6 +102,10 @@ public String remove(String key) {
return headersMap.remove(key);
}

public void clear() {
headersMap.clear();
}

public Set<String> getNames() {
return headersMap.keySet();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ public void removeFile(String key) {
fileParamsMap.remove(key);
}

public void remove(String key) {
removeUrl(key);
removeFile(key);
}

public void clear() {
urlParamsMap.clear();
fileParamsMap.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,6 @@ public R params(String key, File file, String fileName, MediaType contentType) {
return (R) this;
}

@SuppressWarnings("unchecked")
@Override
public R removeFileParam(String key) {
params.removeFile(key);
return (R) this;
}

@Override
protected RequestBody generateRequestBody() {
if (requestBody != null) return requestBody;
Expand Down
Loading

0 comments on commit 669fdb9

Please sign in to comment.