Skip to content

Commit

Permalink
版本升级1.6.0,扩展请求功能,一个key可上传多个参数或者文件,增加请求头user-agent等可全局设置的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
jeasonlzy committed Jun 19, 2016
1 parent 2f27842 commit 7629249
Show file tree
Hide file tree
Showing 18 changed files with 363 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
*.iml
.gradle
bintray.gradle
/.idea
/screenshots
/local.properties
.DS_Store
/build
/captures
libs/
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

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

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

Expand All @@ -63,6 +63,7 @@
* 一般的 get,post,put,delete,head,options请求
* 基于Post的大文本数据上传
* 多文件和多参数统一的表单上传
* 支持一个key上传一个文件,也可以一个Key上传多个文件
* 大文件下载和下载进度回调
* 大文件上传和上传进度回调
* 支持cookie的内存存储和持久化存储,支持传递自定义cookie
Expand Down Expand Up @@ -143,7 +144,7 @@ OkHttpUtils.get(Urls.URL_DOWNLOAD)//
});
```
### 4.普通Post,直接上传String类型的文本
不建议这么用,该方法上传字符串会清空实体中其他所有的参数,但头信息不清除
一般此种用法用于与服务器约定的数据格式,当使用该方法时,params中的参数设置是无效的,所有参数均需要通过需要上传的文本中指定,此外,额外指定的header参数仍然保持有效。
```java
OkHttpUtils.post(Urls.URL_TEXT_UPLOAD)//
.tag(this)//
Expand All @@ -157,7 +158,7 @@ OkHttpUtils.post(Urls.URL_TEXT_UPLOAD)//
```

### 5.普通Post,直接上传Json类型的文本
不建议这么用,该方法上传字符串会清空实体中其他所有的参数,但头信息不清除
该方法与postString没有本质区别,只是数据格式是json,一般来说,需要自己创建一个实体bean或者一个map,把需要的参数设置进去,然后通过三方的Gson或者fastjson转换成json字符串,最后直接使用该方法提交到服务器。
```java
OkHttpUtils.post(Urls.URL_TEXT_UPLOAD)//
.tag(this)//
Expand Down Expand Up @@ -196,6 +197,9 @@ OkHttpUtils.get(Urls.URL_METHOD) // 请求方式和请求url, get请求不需要
.params("param2", "paramValue2") // 支持多请求参数同时添加
.params("file1", new File("filepath1")) // 可以添加文件上传
.params("file2", new File("filepath2")) // 支持多文件同时添加上传
.addUrlParams("key", List<String> values) //这里支持一个key传多个参数
.addFileParams("key", List<File> files) //这里支持一个key传多个文件
.addFileWrapperParams("key", List<HttpParams.FileWrapper> fileWrappers) //这里支持一个key传多个文件
.addCookie("aaa", "bbb") // 这里可以传递自己想传的Cookie
.addCookie(cookie) // 可以自己构建cookie
.addCookies(cookies) // 可以一次传递批量的cookie
Expand Down
6 changes: 3 additions & 3 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 17
versionName "1.5.2"
versionCode 18
versionName "1.6.0"
}
buildTypes {
release {
Expand Down Expand Up @@ -40,7 +40,7 @@ dependencies {
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.google.code.gson:gson:2.5'

compile 'com.lzy.widget:imagepicker:0.2.1'
compile 'com.lzy.widget:imagepicker:0.2.6'
compile 'com.lzy.widget:view-core:0.2.1'
// compile 'com.lzy.net:okhttputils:+'
// compile 'com.lzy.net:okhttpserver:+'
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/com/lzy/okhttpdemo/GApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.app.Application;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.cookie.store.MemoryCookieStore;
import com.lzy.okhttputils.cookie.store.PersistentCookieStore;
import com.lzy.okhttputils.model.HttpHeaders;
import com.lzy.okhttputils.model.HttpParams;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.lzy.okhttpdemo.utils.Urls;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.request.BaseRequest;
import com.lzy.okhttputils.request.PostRequest;

import java.io.File;
import java.util.ArrayList;
Expand Down Expand Up @@ -80,8 +79,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (imageItems != null && imageItems.size() > 0) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < imageItems.size(); i++) {
if (i == imageItems.size() - 1)
sb.append("图片").append(i + 1).append(" : ").append(imageItems.get(i).path);
if (i == imageItems.size() - 1) sb.append("图片").append(i + 1).append(" : ").append(imageItems.get(i).path);
else sb.append("图片").append(i + 1).append(" : ").append(imageItems.get(i).path).append("\n");
}
tvImages.setText(sb.toString());
Expand All @@ -97,21 +95,24 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {

@OnClick(R.id.formUpload)
public void formUpload(View view) {
ArrayList<File> files = new ArrayList<>();
if (imageItems != null && imageItems.size() > 0) {
for (int i = 0; i < imageItems.size(); i++) {
files.add(new File(imageItems.get(i).path));
}
}
//拼接参数
PostRequest request = OkHttpUtils.post(Urls.URL_FORM_UPLOAD)//
OkHttpUtils.post(Urls.URL_FORM_UPLOAD)//
.tag(this)//
.headers("header1", "headerValue1")//
.headers("header2", "headerValue2")//
.params("param1", "paramValue1")//
.params("param2", "paramValue2");//
//拼接选中的文件参数(如果文件个数已知,可以链式调用到底,就不用这么断开了)
if (imageItems != null && imageItems.size() > 0) {
for (int i = 0; i < imageItems.size(); i++) {
request.params("file" + (i + 1), new File(imageItems.get(i).path));
}
}
//执行请求
request.execute(new ProgressUpCallBack<>(this, RequestInfo.class));
.params("param2", "paramValue2")//
// .params("file1",new File("文件路径")) //这种方式为一个key,对应一个文件
// .params("file2",new File("文件路径"))
// .params("file3",new File("文件路径"))
.addFileParams("file", files) // 这种方式为同一个key,上传多个文件
.execute(new ProgressUpCallBack<>(this, RequestInfo.class));
}

private class ProgressUpCallBack<T> extends JsonCallback<T> {
Expand Down
37 changes: 31 additions & 6 deletions app/src/main/java/com/lzy/okhttpdemo/activity/TestActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@

import com.lzy.okhttpdemo.R;
import com.lzy.okhttpdemo.callback.StringDialogCallback;
import com.lzy.okhttpdemo.utils.Urls;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import java.io.File;
import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Request;
Expand All @@ -26,8 +30,15 @@ protected void onCreate(Bundle savedInstanceState) {

@OnClick(R.id.btn1)
public void btn1(View view) {
OkHttpUtils.get("http://www.qunar.com")//
ArrayList<String> params = new ArrayList<>();
params.add("111");
params.add("222");
params.add("333");
params.add("444");
OkHttpUtils.get(Urls.URL_METHOD)//
.tag(this)//
.addUrlParams("aaa",params)
.params("ddd","444")
.execute(new StringCallback() {
@Override
public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
Expand All @@ -37,11 +48,17 @@ public void onResponse(boolean isFromCache, String s, Request request, @Nullable

@OnClick(R.id.btn2)
public void btn2(View view) {
OkHttpUtils.post("http://dev.11yuehui.com/WebApi/Login/login.html")//
ArrayList<String> params = new ArrayList<>();
params.add("111");
params.add("222");
params.add("333");
params.add("444");
OkHttpUtils.post(Urls.URL_METHOD)//
.tag(this)//
.params("username", "273029") //用户名
.params("password", "273029tc") //密码
.execute(new StringDialogCallback(this) {
.addUrlParams("aaa",params)
.params("bbb","666")
.params("ccc","777")
.execute(new StringCallback() {
@Override
public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
}
Expand All @@ -50,6 +67,14 @@ public void onResponse(boolean isFromCache, String s, Request request, @Nullable

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

OkHttpUtils.post("http://dev.11yuehui.com/WebApi/Login/login.html")//
.tag(this)//
.params("username", "273029") //用户名
.params("password", "273029tc") //密码
.execute(new StringDialogCallback(this) {
@Override
public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.lzy.okhttputils.request.BaseRequest;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
Expand Down Expand Up @@ -39,7 +40,7 @@ private void sign(HttpParams params) {
params.put("nonce", getRndStr(6 + RANDOM.nextInt(8)));
params.put("timestamp", "" + (System.currentTimeMillis() / 1000L));
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : getSortedMapByKey(params.urlParamsMap).entrySet()) {
for (Map.Entry<String, List<String>> entry : getSortedMapByKey(params.urlParamsMap).entrySet()) {
sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
}
sb.delete(sb.length() - 1, sb.length());
Expand All @@ -59,15 +60,15 @@ private String getRndStr(int length) {
}

/** 按照key的自然顺序进行排序,并返回 */
private Map<String, String> getSortedMapByKey(ConcurrentHashMap<String, String> map) {
private Map<String, List<String>> getSortedMapByKey(ConcurrentHashMap<String, List<String>> map) {
Comparator<String> comparator = new Comparator<String>() {
@Override
public int compare(String lhs, String rhs) {
return lhs.compareTo(rhs);
}
};
TreeMap<String, String> treeMap = new TreeMap<>(comparator);
for (Map.Entry<String, String> entry : map.entrySet()) {
TreeMap<String, List<String>> treeMap = new TreeMap<>(comparator);
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
treeMap.put(entry.getKey(), entry.getValue());
}
return treeMap;
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/lzy/okhttpdemo/utils/Urls.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
public class Urls {
public static final String SERVER = "http://server.jeasonlzy.com/OkHttpUtils/";
// public static final String SERVER = "http://192.168.1.102:8080/OkHttpUtils/";
// public static final String SERVER = "http://192.168.1.121:8080/OkHttpUtils/";
public static final String URL_METHOD = SERVER + "method";
public static final String URL_CACHE = SERVER + "cache";
public static final String URL_IMAGE = SERVER + "image";
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
classpath 'com.android.tools.build:gradle:2.1.2'

classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
Expand Down
Binary file not shown.
100 changes: 100 additions & 0 deletions okhttpserver/bintray.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

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

def siteUrl = 'https://github.com/jeasonlzy0216/OkHttpUtils'
def gitUrl = 'https://github.com/jeasonlzy0216/OkHttpUtils.git'
group = "com.lzy.net" // 数据仓库依赖第一部分

install {
repositories.mavenInstaller {
pom {
project {
packaging 'aar'
name 'okhttpserver For Android' // 项目描述
url siteUrl
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id 'jeasonlzy' // 开发者信息
name 'LiaoZiYao' // 开发者信息
email '[email protected]' // 开发者信息
}
}
scm {
connection gitUrl
developerConnection gitUrl
url siteUrl
}
}
}
}
}

task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}

task javadoc(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}

task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}

javadoc {
options {
encoding "UTF-8"
charSet 'UTF-8'
author true
version true
links "http://docs.oracle.com/javase/7/docs/api"
title 'okhttpserver For Android' // 文档标题
}
}

artifacts {
// archives javadocJar
archives sourcesJar
}

//定义一个函数,target是生成jar包的文件名
def makeJar(String target) {
exec {
executable "jar" //调用jar
args "cvf", target
args "-C", "build/intermediates/classes/release"
args "", "."
}
}

//新建一个task,名为buildLib,依赖build(build是一个自带的task)
task buildLib(dependsOn: build) {
makeJar("../lib/okhttpserver-" + version + ".jar")
}

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
bintray {
user = properties.getProperty("bintray.user")
key = properties.getProperty("bintray.apikey")
configurations = ['archives']
pkg {
repo = "maven"
name = "okhttpserver" // 数据仓库依赖第二部分
websiteUrl = siteUrl
vcsUrl = gitUrl
licenses = ["Apache-2.0"]
publish = true
}
}
Loading

0 comments on commit 7629249

Please sign in to comment.