nohttp网络框架
发布日期:2021-05-16 19:05:27 浏览次数:23 分类:精选文章

本文共 3594 字,大约阅读时间需要 11 分钟。

Android网络请求框架NoHttp文档

NoHttp是一个Android Http协议标准框架,支持多种缓存模式,底层可动态切换OkHttp、URLConnection,与RxJava完美结合。开发者可以根据需求选择使用URLConnection或OkHttp作为网络层。


NoHttp框架特性

功能特点

  • 动态配置网络层:支持OkHttp和URLConnection切换。
  • 支持异步与同步请求:适用于多种业务场景。
  • 多任务处理:支持多文件上传、大文件上传和下载,提供进度回调。
  • 丰富数据支持:支持String、Bitmap、JsonArray等多种数据类型的GET与POST请求,文件上传下载功能。
  • 灵活缓存模式:可根据需求选择数据库、SD卡或自定义路径存储缓存,缓存数据加密。
  • 自定义请求:支持直接请求JavaBean、List、Map等复杂数据类型。
  • Cookie管理:支持自动维持Cookie,包括App重启、关机后的Cookie存续。
  • Http协议支持:涵盖301、302、307等多种重定向,支持Https访问。
  • 失败重试机制:支持重试策略和请求优先级配置。
  • 请求队列:支持多线程并发,提供请求取消功能。
  • 高效权限管理:集成运行时权限申请,确保缓存存储自行定,即使在6.0及以上版本也能顺利运行。

依赖管理

  • 默认使用URLConnection

    implementation 'com.yanzhenjie.nohttp:nohttp:1.1.11'
  • 切换到OkHttp网络层

    implementation 'com.yanzhenjie.nohttp:okhttp:1.1.11'

初始化配置

简单初始化

NoHttp.initialize(this);

高级初始化配置

  • 全局设置超时:
    InitializationConfig config = InitializationConfig.newBuilder(context)
    .connectionTimeout(30_000) // 超时 milliseconds
    .readTimeout(30_000) // 响应超时 milliseconds
    .cacheStore(new DBCacheStore(context).setEnable(true))
    .cookieStore(new DBCookieStore(context).setEnable(true))
    .networkExecutor()
    .addHeader("Token", "123")
    .addParam("AppVersion", "1.0.0")
    .build();

缓存配置

  • 默认缓存存储路径:

    InitializationConfig config = InitializationConfig.newBuilder(context)
    .cacheStore(new DiskCacheStore(context));
  • 自定义缓存目录:

    InitializationConfig config = InitializationConfig.newBuilder(context)
    .cacheStore(new DiskCacheStore("/path/to/custom directories"));

请求方式

同步请求示例

StringRequest request = new StringRequest("http://api.nohttp.net", RequestMethod.POST);
Response<(jsonObject>> response = SyncRequestExecutor.INSTANCE.execute(request);
if (response.isSucceed()) {
// 处理成功响应
} else {
// 处理失败响应
Exception e = response.getException();
}

异步请求方式

使用AsyncRequestExecutor

Cancelable cancel = AsyncRequestExecutor.INSTANCE.execute(
0, // 根据需求设置优先级
request,
new SimpleResponseListener() {
@Override
public void onSucceed(int what, Response<...> response) {
// 处理成功
}
@Override
public void onFailed(int what, Response<...> response) {
// 处理失败
}
}
);

使用RequestQueue

RequestQueue queue = NoHttp.newRequestQueue();
queue.add(0, request, listener);
// 提前释放队列资源
queue.stop();

功能扩展

URL拼装

String url = "http://api.nohttp.net/rest/";
url = url + "user/" + username + "/info";

请求头和参数

  • 添加请求头:

    request.addHeader("Token", "123");
    request.addHeader("Token", "456"); // 多次添加不会覆盖
  • 添加请求参数:

    request.add("AppVersion", "1.0.0");
    request.add("AppType", "Android");
    request.add("AppType", "iOS"); // 不会覆盖

文件上传与下载

  • 单文件上传:

    request.add("file", new FileBinary(file));
  • 多文件上传:

    request.add("file", new FileBinary(file))
    .add("image", new InputStreamBinary(inputStream));
  • 下载示例:

    DownloadRequest downloadRequest = new DownloadRequest(url, RequestMethod.GET, folder, filename, true, true);
    SyncDownloadExecutor.INSTANCE.execute(0, downloadRequest, listener);

序列化与反序列化

NoHttp支持自定义请求数据类型,例如:

public class FastJsonRequest extends RestRequest
{
public FastJsonRequest(String url) {
this(url, RequestMethod.GET);
}
public FastJsonRequest(String url, RequestMethod requestMethod) {
super(url, requestMethod);
}
@Override
public JSONObject parseResponse(Headers headers, byte[] body) {
String jsonStr = StringRequest.parseResponseString(headers, body);
return JSON.parseObject(jsonStr);
}
}

运行时权限

为确保SD卡写入权限,建议在AndroidManifest.xml中添加以下权限:


调试模式

启用调试模式以查看详细日志:

Logger.setDebug(true);
Logger.setTag("NoHttpDebug");

通过以上配置和功能,NoHttp能够满足Android应用程序在Http网络请求中的多样化需求,帮助开发者高效解决实际问题。

上一篇:nohttp实战
下一篇:什么是ERP

发表评论

最新留言

感谢大佬
[***.8.128.20]2025年04月29日 09时22分36秒