3 Star 3 Fork 1

pengxiao18 / okhttpX

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

#okhttpX ####okhttpX 是一个Android网络层框架,包装了okhttp3.x。

主要功能:
1.可以将JSON字符串转换为java对象
2.对post缓存支持,可以很随意设置接口缓存

###使用方法 #####1.初始化缓存目录

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        // 缓存目录
        HttpClient.cachePath = getAppCacheDir() + File.separator + "httpCache";
    }

    /**
     * 获取缓存目录
     */
    public String getAppCacheDir(){
        String cachePath;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                || !Environment.isExternalStorageRemovable()) {
            cachePath = getExternalCacheDir().getPath();
        } else {
            cachePath = getCacheDir().getPath();
        }
        return cachePath;
    }
}

#####2.实现自己的API服务,继承BaseHttpService。例如要实现一个UserSerive:

public class UserService extends BaseHttpService {

    private static UserService instance;

    private UserService() {
    }

    public static UserService getInstance() {
        if (instance == null) {
            instance = new UserService();
        }
        return instance;
    }

    private static final String getUserById = "http://192.168.1.165:8080/test/user";

    /**
     * 获取用户信息
     */
    public void getUserById(int userId, OnResponseListener listener) {
        Map<String, Object> map = getMap();
        map.put("uid", userId);
        RequestParam param = getRequestParam(getUserById, map);
        ResultType resultType = getResultType(ResultType.Type.OBJECT, UserBean.class);
        CacheTime cacheTime = getCacheTime(TimeUnit.SECONDS, 10);
        executeRequest(param, resultType, cacheTime, listener);
    }
}

#####3.使用UserService

private void testHttp() {
        UserService.getInstance().getUserById(13, new OnUIResponseListener(this, this) {
            @Override
            public void callUISuccess(ResultObject resultObject) {
                UserBean user = (UserBean) resultObject.getData();
                tv_json.setText(user.getId() + " , " + user.getName());
            }

            @Override
            public void callUIFail() {
                Toast.makeText(getApplicationContext(), "失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

空文件

简介

okhttpx注重项目实用性,是okHttp3.6.0的扩展,支持包括post请求在内的staleTime缓存,支持json字符串映射为Object。 展开 收起
Android
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Android
1
https://gitee.com/pengxiao/okhttpX.git
git@gitee.com:pengxiao/okhttpX.git
pengxiao
okhttpX
okhttpX
master

搜索帮助