创意封装OkHttp,简单易用,你还在等什么呢?

在实际开发中,我们大概用到的就是get、post、上传、下载这四个网络请求了。amhttp框架针对于这四种需求对应产生了AMQuery、AMPost、AMUpload、AMDownload四个对象,再次封装OKHttp。支持链式请求。看到这些对象就知道要做的事情了。不需要再去设置httpmethod,配置参数即可。

用法

在project/module的build.gradle中添加:

compile 'io.chelizi:amhttp:1.0.6'

服务端测试环境

git地址:https://github.com/Eddieyuan123/amhttp-server.git
这个项目是用来测试amhttp的,可以clone下来测试。(服务端代码采用springboot + springMVC + gradle)。

1.AMQuery 查询(get)

构建一个AMQuery对象,不需要关心内部如何实现,配置相应的参数即可。对于response返回的数据经Gson映射成实体。使用起来简单方便。支持同步异步请求。

 AMQuery query = new AMQuery<>();
        query.setUrl("http://192.168.1.10:8090/blog/id?id=1")
             .setCacheControl(CacheControl.FORCE_NETWORK)
             .setCallMethod(CallMethod.SYNC)//同步
             .setTag(hashCode());
        query.findObjects(this, new OnFindListener() {
            @Override
            public void onResponseSuccess(Blog response) {
                Toast.makeText(MainActivity.this,response.getTitle(),Toast.LENGTH_LONG).show();
            }

            @Override
            public void onResponseError(int code, @Nullable HttpError httpError) {

            }

            @Override
            public void onFailure(Exception e) {

            }
        });

2. AMPost 提交

构建一个AMPost对象。有两种提交参数的方法,1.addWhereEqualTo(key,value) 2.setParams(String params).目前只支持任选其一。

 AMPost post = new AMPost<>();
        post.setUrl("http://192.168.43.36:8090/blog/save")
            .addWhereEqualTo("title","最新报道")
            .addWhereEqualTo("content","tianjin")
            .setCacheControl(CacheControl.FORCE_NETWORK)
            .setTag(hashCode());

        post.addObjects(this, new OnAddListener() {
            @Override
            public void onResponseSuccess(String response) {

            }

            @Override
            public void onResponseError(int code, @Nullable HttpError httpError) {

            }

            @Override
            public void onFailure(Exception e) {

            }
        });

3.AMUpload 上传

构造一个AMUpload对象。文件上传。如果传的Base64,则可以用AMPost对象。

         AMUpload upload = new AMUpload<>();
        upload.setUrl("http://192.168.1.9:8090/blog/upload")
                .setFile(new File(getFilesDir().getAbsolutePath() + "/image.jpg"))
                .setFileName("image.jpg")
                .addWhereEqualTo("image","mark");

        upload.uploadObjects(this, new OnUploadListener() {
            @Override
            public void onRequestProgress(long progress, long total, boolean done) {
                Log.d("upload_success","progress= " + progress + ",total = " + total);
            }

            @Override
            public void onResponseSuccess(Size response) {
                Toast.makeText(MainActivity.this,response.getDesc(),Toast.LENGTH_LONG).show();
            }

            @Override
            public void onResponseError(int code, HttpError httpError) {

            }

            @Override
            public void onFailure(Exception e) {

            }
        });

4.AMDownload 下载

  构造一个AMDownload对象,设置FildCard.支持下载进度条。最后生成file对象。

AMDownload download = new AMDownload<>();
        download.setUrl("http://img0.utuku.china.com/550x0/news/20170528/1b3b24eb-44d4-4548-a40a-e6c089f6b4db.jpg")
                .setFileCard(new FileCard(
                        getCacheDir().getAbsolutePath(),
                        System.currentTimeMillis() + ".jpg"));

        download.downloadObjects(this, new OnDownloadListener() {

            @Override
            public void onProgressChanged(long progress, long total) {
                Log.d(MainActivity.class.getSimpleName(),"progress = " + progress + ",total = " + total );
            }

            @Override
            public void onResponseSuccess(File response) {
                Glide.with(MainActivity.this).load(response.getAbsolutePath()).into(
                        imageView);
            }

            @Override
            public void onResponseError(int code, @Nullable HttpError httpError) {

            }

            @Override
            public void onFailure(Exception e) {

            }
        });

你可能感兴趣的:(【android进阶】)