Skip to content

Commit

Permalink
一次合并(未解决冲突)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeasonlzy committed Sep 28, 2016
2 parents 7786251 + 4311eaa commit a7d308f
Show file tree
Hide file tree
Showing 14 changed files with 152 additions and 28 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@

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

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

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

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

或者

Expand All @@ -50,8 +50,8 @@

* 对于Eclipse的用户,可以选择添加 `/jar` 目录下的:
```java
okhttputils-1.8.0.jar
okhttpserver-1.0.2.jar
okhttputils-1.8.1.jar
okhttpserver-1.0.3.jar
```
* 如果是以jar包的形式引入`okhttpserver`,需要在清单文件中额外注册一个服务
```java
Expand Down Expand Up @@ -314,6 +314,7 @@ OkHttpUtils.get("https://kyfw.12306.cn/otn")//
以下代码包含了以下内容:

* 一次普通请求所有能配置的参数,真实使用时不需要配置这么多,按自己的需要选择性的使用即可
* `params`添加参数的时候,最后一个`isReplace`为可选参数,默认为`true`,即代表相同`key`的时候,后添加的会覆盖先前添加的
* 多文件和多参数的表单上传,同时支持进度监听
* 自签名网站https的访问,调用`setCertificates`方法即可
* 为单个请求设置超时,比如涉及到文件的需要设置读写等待时间多一点。
Expand Down
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ android {
defaultConfig {
applicationId "com.lzy.okhttpdemo"
minSdkVersion 14
targetSdkVersion 24
versionCode 22
versionName "1.8.0"
targetSdkVersion 22
versionCode 23
versionName "1.8.1"
}
signingConfigs {
appkey {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected void initData() {
public void onRefresh() {
OkHttpGo.get(Urls.NEWS)//
.params("channelName", fragmentTitle)//
.params("page", String.valueOf(1)) //初始化或者下拉刷新,默认加载第一页
.params("page", 1) //初始化或者下拉刷新,默认加载第一页
.cacheKey("TabFragment_" + fragmentTitle) //由于该fragment会被复用,必须保证key唯一,否则数据会发生覆盖
.cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST) //缓存模式先使用缓存,然后使用网络数据
.execute(new NewsCallback<NewsModel>(NewsModel.class) {
Expand Down Expand Up @@ -124,8 +124,8 @@ public void onAfter(@Nullable NewsModel newsModel, @Nullable Exception e) {
public void onLoadMoreRequested() {
OkHttpGo.get(Urls.NEWS)//
.params("channelName", fragmentTitle)//
.params("page", String.valueOf(currentPage + 1)) //上拉加载更多
.cacheMode(CacheMode.NO_CACHE) //上拉不需要缓存
.params("page", currentPage + 1) //上拉加载更多
.cacheMode(CacheMode.NO_CACHE) //上拉不需要缓存
.execute(new NewsCallback<NewsModel>(NewsModel.class) {
@Override
public void onSuccess(NewsModel newsModel, Call call, Response response) {
Expand Down
70 changes: 70 additions & 0 deletions app/src/main/java/com/lzy/okhttpdemo/okhttputils/TestActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.lzy.okhttpdemo.okhttputils;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.lzy.okhttpdemo.R;
import com.lzy.okhttpdemo.base.BaseActivity;
import com.lzy.okhttpdemo.callback.DialogCallback;
import com.lzy.okhttpdemo.model.ServerModel;
import com.lzy.okhttpdemo.utils.Urls;
import com.lzy.okhttputils.OkHttpUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class TestActivity extends BaseActivity {

@Bind(R.id.image) ImageView imageView;
@Bind(R.id.edit) EditText editText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
setTitle("测试页面");
ButterKnife.bind(this);
}

@Override
protected void onDestroy() {
super.onDestroy();
//Activity销毁时,取消网络请求
OkHttpUtils.getInstance().cancelTag(this);
}

@OnClick(R.id.btn1)
public void btn1(View view) {

OkHttpUtils.get(Urls.URL_METHOD)//
.tag(this)//
.headers("header1", "headerValue1")//
.params("param1", "paramValue1")//
.execute(new DialogCallback<ServerModel>(this, ServerModel.class) {
@Override
public void onSuccess(ServerModel serverModel, Call call, Response response) {
System.out.println("onSuccess -- " + serverModel);
}

@Override
public void onError(Call call, Response response, Exception e) {
super.onError(call, response, e);
System.out.println("onError");
}
});

}

@OnClick(R.id.btn2)
public void btn2(View view) {
}

@OnClick(R.id.btn3)
public void btn3(View view) {
}
}
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion okhttpgo/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.8.0" // 数据仓库依赖第三部分
version = "1.8.1" // 数据仓库依赖第三部分

def siteUrl = 'https://github.com/jeasonlzy0216/OkHttpGo'
def gitUrl = 'https://github.com/jeasonlzy0216/OkHttpGo.git'
Expand Down
6 changes: 3 additions & 3 deletions okhttpgo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ android {

defaultConfig {
minSdkVersion 8
targetSdkVersion 24
versionCode 16
versionName "1.8.0"
targetSdkVersion 22
versionCode 17
versionName "1.8.1"
}
}

Expand Down
64 changes: 56 additions & 8 deletions okhttpgo/src/main/java/com/lzy/okhttpgo/model/HttpParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,70 @@ public void put(HttpParams params) {
}
}

public void put(Map<String, String> params) {
put(params, IS_REPLACE);
}

public void put(Map<String, String> params, boolean isReplace) {
public void put(Map<String, String> params, boolean... isReplace) {
if (params == null || params.isEmpty()) return;
for (Map.Entry<String, String> entry : params.entrySet()) {
put(entry.getKey(), entry.getValue(), isReplace);
}
}

public void put(String key, String value) {
put(key, value, IS_REPLACE);
public void put(String key, String value, boolean... isReplace) {
if (isReplace != null && isReplace.length > 0) {
put(key, String.valueOf(value), isReplace[0]);
} else {
put(key, String.valueOf(value), IS_REPLACE);
}
}

public void put(String key, int value, boolean... isReplace) {
if (isReplace != null && isReplace.length > 0) {
put(key, String.valueOf(value), isReplace[0]);
} else {
put(key, String.valueOf(value), IS_REPLACE);
}
}

public void put(String key, long value, boolean... isReplace) {
if (isReplace != null && isReplace.length > 0) {
put(key, String.valueOf(value), isReplace[0]);
} else {
put(key, String.valueOf(value), IS_REPLACE);
}
}

public void put(String key, float value, boolean... isReplace) {
if (isReplace != null && isReplace.length > 0) {
put(key, String.valueOf(value), isReplace[0]);
} else {
put(key, String.valueOf(value), IS_REPLACE);
}
}

public void put(String key, double value, boolean... isReplace) {
if (isReplace != null && isReplace.length > 0) {
put(key, String.valueOf(value), isReplace[0]);
} else {
put(key, String.valueOf(value), IS_REPLACE);
}
}

public void put(String key, char value, boolean... isReplace) {
if (isReplace != null && isReplace.length > 0) {
put(key, String.valueOf(value), isReplace[0]);
} else {
put(key, String.valueOf(value), IS_REPLACE);
}
}

public void put(String key, boolean value, boolean... isReplace) {
if (isReplace != null && isReplace.length > 0) {
put(key, String.valueOf(value), isReplace[0]);
} else {
put(key, String.valueOf(value), IS_REPLACE);
}
}

public void put(String key, String value, boolean isReplace) {
private void put(String key, String value, boolean isReplace) {
if (key != null && value != null) {
List<String> urlValues = urlParamsMap.get(key);
if (urlValues == null) {
Expand Down
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.2" // 数据仓库依赖第三部分
version = "1.0.3" // 数据仓库依赖第三部分

def siteUrl = 'https://github.com/jeasonlzy0216/OkHttpGo'
def gitUrl = 'https://github.com/jeasonlzy0216/OkHttpGo.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,14 +7,14 @@ android {
defaultConfig {
minSdkVersion 8
targetSdkVersion 24
versionCode 12
versionName "1.0.2"
versionCode 13
versionName "1.0.3"
}
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,16 @@ public void stopAllTask() {

/** 删除一个任务,会删除下载文件 */
public void removeTask(String taskKey) {
removeTask(taskKey, false);
}

/** 删除一个任务,会删除下载文件 */
public void removeTask(String taskKey, boolean isDeleteFile) {
final DownloadInfo downloadInfo = getDownloadInfo(taskKey);
if (downloadInfo == null) return;
pauseTask(taskKey); //暂停任务
removeTaskByKey(taskKey); //移除任务
deleteFile(downloadInfo.getTargetPath()); //删除文件
if (isDeleteFile) deleteFile(downloadInfo.getTargetPath()); //删除文件
DownloadDBManager.INSTANCE.delete(taskKey); //清除数据库
}

Expand Down
Binary file removed okhttputils_v1.8.0.apk
Binary file not shown.
Binary file added okhttputils_v1.8.1.apk
Binary file not shown.

0 comments on commit a7d308f

Please sign in to comment.