Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.cat9:EasyVolley:'
}
dependencies {
implementation("com.github.cat9:EasyVolley:")
}
<dependency>
<groupId>com.github.cat9</groupId>
<artifactId>EasyVolley</artifactId>
<version></version>
</dependency>
libraryDependencies += "com.github.cat9" % "EasyVolley" % ""
:dependencies [[com.github.cat9/EasyVolley ""]]
EasyVolley library,Improve from google Volley,make it easier to program,for upload files,download files.
###Normal Request
RequestParams params = new RequestParams();
params.addBodyParameter("session_id", "4095c7d3c0c9240aa6975044");
params.addBodyParameter("birthday", "1986-01-01");
String url = "http://yourhost/index.php?act=User&m=updateProfile";
StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e(TAG, response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
request.setRequestParams(params);
request.setTag(TAG);
mRequestQueue.add(request);
###FilesUpload Request
RequestParams params = new RequestParams();
params.addBodyParameter("session_id", "4095c7d3c0c9240aa6975044");
params.addBodyParameter("hello_video", new File("/sdcard/20150914_153044.mp4"));
params.addBodyParameter("hello_pic", new File("/sdcard/20150914_153044.jpg"));
String url = "http://yourhost/index.php?act=ActorSetting&m=updateVideo";
StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e(TAG, response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
request.setLoadingListener(new Response.LoadingListener() {
@Override
public void onLoading(boolean isUpload, long total, long current) {
Log.e(TAG, "isUpload:" + isUpload + ",total:" + total + ",current:" + current);
}
});
request.setRequestParams(params);
request.setTag(TAG);
mRequestQueue.add(request);
###FileDownload Request FileDownload support continue download where you canceled it.
DownloadRequest(int method, String url, String fileSavePath, boolean autoResume,
Response.Listener<File> listener, Response.ErrorListener errorListener);
String url = "http://yourhost/ff.apk";
DownloadRequest request = new DownloadRequest(Request.Method.GET, url, "/sdcard/out.apk", false, new Response.Listener<File>() {
@Override
public void onResponse(File response) {
Log.e(TAG, "download finish:" + response.getAbsolutePath());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
request.setLoadingListener(new Response.LoadingListener() {
@Override
public void onLoading(boolean isUpload, long total, long current) {
Log.e(TAG, "isUpload:" + isUpload + ",total:" + total + ",current:" + current);
}
});
request.setTag(TAG);
mRequestQueue.add(request);
##Request cancel you can cancel the request by using:
request.cancel();
or cancel all request by TAG,this can be very useful when your Activity is about to destroy!
mRequestQueue.cancelAll(TAG);
if you have any problem,welcome to contact me via my email:lxr309@gmail.com or give me a feedback on my blog: http://blog.csdn.net/mikulee/article/details/48522085
EasyVolley库是Google Volley的改良版本,它简化了请求参数处理,添加对文件的上传及下载的支持,其他相似库,例如VolleyPlus等,它们是把要上传及下载的东西,全部读取到内存,然后再发送出去或保存到文件,这必然会出现内存损耗过多的问题,如果是大文件,手机必然处理不过来。
###普通请求
RequestParams params = new RequestParams();
params.addBodyParameter("session_id", "4095c7d3c0c9240aa6975044");
params.addBodyParameter("birthday", "1986-01-01");
String url = "http://yourhost/index.php?act=User&m=updateProfile";
StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e(TAG, response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
request.setRequestParams(params);
request.setTag(TAG);
mRequestQueue.add(request);
###文件上传请求
RequestParams params = new RequestParams();
params.addBodyParameter("session_id", "4095c7d3c0c9240aa6975044");
params.addBodyParameter("hello_video", new File("/sdcard/20150914_153044.mp4"));
params.addBodyParameter("hello_pic", new File("/sdcard/20150914_153044.jpg"));
String url = "http://yourhost/index.php?act=ActorSetting&m=updateVideo";
StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e(TAG, response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
request.setLoadingListener(new Response.LoadingListener() {
@Override
public void onLoading(boolean isUpload, long total, long current) {
Log.e(TAG, "isUpload:" + isUpload + ",total:" + total + ",current:" + current);
}
});
request.setRequestParams(params);
request.setTag(TAG);
mRequestQueue.add(request);
###文件下载请求 文件下载支持断点续传机制,参数autoResume
DownloadRequest(int method, String url, String fileSavePath, boolean autoResume,
Response.Listener<File> listener, Response.ErrorListener errorListener);
String url = "http://yourhost/ff.apk";
DownloadRequest request = new DownloadRequest(Request.Method.GET, url, "/sdcard/out.apk", false, new Response.Listener<File>() {
@Override
public void onResponse(File response) {
Log.e(TAG, "download finish:" + response.getAbsolutePath());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
request.setLoadingListener(new Response.LoadingListener() {
@Override
public void onLoading(boolean isUpload, long total, long current) {
Log.e(TAG, "isUpload:" + isUpload + ",total:" + total + ",current:" + current);
}
});
request.setTag(TAG);
mRequestQueue.add(request);
##取消请求 你可以在任何地方使用如下方法取消请求。
request.cancel();
或者使用TAG来取消请求。当你的Activity要被Destory时,把该Activity的相关请求取消掉,是一个相当好的习惯!
mRequestQueue.cancelAll(TAG);
如果有任何问题,欢迎讨论解决:lxr309@gmail.com 或者到我的博客上留言:http://blog.csdn.net/mikulee/article/details/48522085