题记——
静坐窗前,与明月为伴。
每一天都是一个新的开始,每一天都是一个新的心态,每一天的目标都是品好每一杯白开水。
生活的价值是活的有意义,而活的有意义就是生命的折腾。
在功夫的世界中,唯快不破,在移动的世界中,同样是唯快不破。
在功夫的世界中,最高境界就是无招,那么在移动的世界中,就是一行代码搞定一切。
Nohttp一个大神所创的一行代码搞定一个网络请求。
1 、开启传奇体验之路 ——Nohttp的基本使用步骤
、
2.Post提交参数String响应请求
在网络请求的世界中,解析JSON数据已是一种常态,而JSON只是一个String
/**
* Post请求,返回的数据类型为String
* @param url 请求服务器地址
* @param what 请求服务标识
* @param map 请求参数添加
* @param listener 响应结果监听
*/
public void asyncPostStringRequest(String url, int what,
Map<String, String> map,
OnResponseListener<String> listener) {
/**
* 取消队列中已开始的请求
*/
mRequestQueue.cancelBySign(what);
/**
* 创建请求对象
*/
Request<String> request =
NoHttp.createStringRequest(url, RequestMethod.POST);
/**
* 添加请求参数
*/
if (map != null) {
for (Map.Entry<String, String> entry : map.entrySet()) {
if (entry.getKey() != null) {
request.add(entry.getKey(), entry.getValue());
}
}
}
/**
* 设置取消请求标识
*/
request.setCancelSign(what);
/**
* what: 当多个请求同时使用同一个OnResponseListener时用来区分请求, 类似handler的what一样
* request: 请求对象
* onResponseListener 回调对象,接受请求结果
*/
mRequestQueue.add(what, request, listener);
}
使用方法:
//请求链接
String url = "http://172.19.1.77:8080/OkhttpAppLication/test";
//请求网络标识
final int strUrlTag = 12;
//请求参数
Map<String,String> map = new HashMap<>();
map.put("username","admiin");
map.put("password","122345");
//调用请求
NohttpUtils.getIntanse(getApplication())
.asyncPostStringRequest(urls,
strUrlTag,map,
new SimpleResponseListener<String>() {
@Override
public void onSucceed(int what, Response<String> response) {
/**
* 请求成功接收服务器返回内容
*/
String respose = response.get();
}
@Override
public void onFailed(int what, String url,
Object tag, Exception exception,
int responseCode, long networkMillis) {
/**
* 请求失败
*/
}
});
3.Post提交参数加载图片
/**
* 加载图片的网络请求
* @param url 请求网络服务器地址
* @param what 请求网络标识
* @param listener 请求网络响应监听
*/
public void asyncLoadImage(String url, int what, OnResponseListener<Bitmap> listener) {
/**
* 创建请求对象
*/
Request<Bitmap> imageRequest = NoHttp.createImageRequest(url);
imageRequest.setTag(what);
/**
* 设置先加载缓存,如果没有缓存,再去加载网络
*/
imageRequest.setCacheMode(CacheMode.NONE_CACHE_REQUEST_NETWORK);
/**
* 设置取消请求标识
*/
imageRequest.setCancelSign(what);
/**
* 发起请求
*/
mRequestQueue.add(what, imageRequest, listener);
}
使用方法:
final String urlimage ="http://pic55.nipic.com/file/20141208/19462408_171130083000_2.jpg";
final ImageView imageView = (ImageView) findViewById(R.id.imageviews);
final int imagTag = 34;
NohttpUtils.getIntanse(getApplication())
.asyncLoadImage(urlimage, imagTag, new SimpleResponseListener<Bitmap>() {
@Override
public void onSucceed(int what, Response<Bitmap> response) {
imageView.setImageBitmap(response.get());
}
@Override
public void onFailed(int what, String url,
Object tag, Exception exception,
int responseCode, long networkMillis) {
}
});
4.下载文件
/**
* @param url 请求服务器链接
* @param what 请求服务标识
* @param map 请求参数封装
* @param filePath 下载文件的保存路径与文件名
* @param listener 下载监听
*/
public void asyncDownFileRequest(String url, int what,
Map<String, String> map,
String filePath, DownloadListener listener) {
/**
* url 下载服务器地址
* RequestMethod.POST 发送请求方式
* Environment.getExternalStorageDirectory().getPath() 文件下载保存路径
* filePath 文件下载保存名称
* true 支持断点下载
* false 不覆盖已有的相同文件名称的文件
*/
DownloadRequest downloadRequest
= NoHttp.createDownloadRequest(url,
RequestMethod.POST,
Environment.getExternalStorageDirectory().getPath(),
filePath, true, false);
/**
* 添加请求参数
*/
if (map != null && !map.isEmpty()) {
for (Map.Entry<String, String> entry : map.entrySet()) {
if (entry.getKey() != null) {
downloadRequest.add(entry.getKey(), entry.getValue());
}
}
}
/**
* 设置请求标识
*/
downloadRequest.setCancelSign(what);
/**
* 下载
*/
downloadQueue.add(what, downloadRequest, listener);
}
使用方法
String downUrl ="http://192.168.1.100:8080//OkhttpAppLication/DownFile1";
int downWhat = 96;
String downFileName ="mewmuise.mp3";
/**
* 下载
*/
NohttpUtils.getIntanse(getApplication()).asyncDownFileRequest(downUrl, downWhat, null, downFileName, new DownloadListener() {
@Override
public void onDownloadError(int what, Exception exception) {
System.out.println("down error "+exception.getMessage());
}
@Override
public void onStart(int what, boolean isResume, long rangeSize, Headers responseHeaders, long allCount) {
}
@Override
public void onProgress(int what, int progress, long fileCount) {
}
@Override
public void onFinish(int what, String filePath) {
System.out.println("down finish "+filePath);
}
@Override
public void onCancel(int what) {
}
});
5.上传表单数据以及文件
/**
* 分块级上传文件 提交表单数据
* @param url
* @param what 网络请求标识
* @param forMap 网络请求提交表单数据
* @param fileMap 网络请求提交上传文件
* @param listener 网络请求标识
*/
public void asyncLoadUpFile(String url, int what, Map<String, String> forMap,Map<String, String> fileMap, OnResponseListener<String> listener) {
mRequestQueue.cancelBySign(what);
Request<String> stringRequest = NoHttp.createStringRequest(url, RequestMethod.POST);
/**
* 添加上传文件信息
*/
if (fileMap != null && !fileMap.isEmpty()) {
for (Map.Entry<String, String> entry : fileMap.entrySet()) {
if (entry.getKey() != null) {
File file = new File(entry.getValue());
String fileName = file.getName();
stringRequest.addHeader("Content-Disposition", "form-data;name=\"" + fileName + "\";filename=\"" + fileName + "\"");
stringRequest.add(entry.getKey(), file);
}
}
}
/**
* 添加请求参数
*/
if (forMap != null && !forMap.isEmpty()) {
for (Map.Entry<String, String> entry : forMap.entrySet()) {
if (entry.getKey() != null) {
stringRequest.add(entry.getKey(), entry.getValue());
}
}
}
/**
* 网络取消标识
*/
stringRequest.setCancelSign(what);
/**
* 发起请求
*/
mRequestQueue.add(what,stringRequest,listener);
}
使用方法
String loadUpUrl ="http://172.19.1.45:8080/OkhttpAppLication/LoadFileServlet";
int what = 58;
Map<String,String> map1 = new HashMap<>();
map1.put("pucture1", "/storage/emulated/0/custom/2016/6/18//20160623135328.PNJE");
/**
* 上传
*/
NohttpUtils.getIntanse(getApplication()).asyncLoadUpFile(loadUpUrl, what, null,map1, new SimpleResponseListener<String>() {
@Override
public void onSucceed(int what, Response<String> response) {
System.out.println("up succeed ");
System.out.println("uploadFiele response is "+response.get());
}
@Override
public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
System.out.println("up faile ");
}
});