安卓开发框架

最近项目一期开发完成,有一点空闲时间,经过一段时间的准备,参考常见的网络应用并对其进行一定的研究,再结合自己的现有的一点点经验,封装了一个android应用开发”框架”,说是”框架”简直是夸下了海口,因为自己的那点小九九不足做出如此高大上的东西来,姑且叫”框架”吧。

“框架”概述
  • async-http-client(发起网络请求)
  • Glide(异步加载网络图片资源)
  • Gson(解析服务器返回的json数据)
  • PullToRefresh(下拉刷新、上拉加载)
发起网络请求
  • 发起一个Get请求(在Activity及Fragment中均只需要通过以下代码发起一个Get请求)
mHttpAsyncTask.getMethod(ServerConfig.INTERVIEW_NOTICE_COUNT, this);
  • 发起一个带参数的Get请求
RequestParams params = new RequestParams();
params.put("userId", "f5a2c82950c70a9c0150cc2d27e3006c");
params.put("toUserId", "f5a2c82950c70a9c0150c32d27e3007c");
params.put("content", "Hello guy!");
mHttpAsyncTask.postMethod(ServerConfig.SEND_TEXT_MESSAGE, this, params, false);
  • 发起一个Post请求
RequestParams params = new RequestParams();
File file = new File("/storage/emulated/0/BaiduNetdisk/img_Leo.png");
try {
    params.put("file", file);
    params.put("name", "Jessie");
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
mHttpAsyncTask.postMethod("http://192.168.198.229/upload.php", this, params);

这里面值得一提的是:服务器URL通常是按模块划分,所以会将URL分割成诸如HTTP_PROTOCAL + MODULE_USER + ACTION这样的结构,如此才能在请求网络数据的回调中对返回结果做出区分,具体还是在使用过程中才深有体会。

  • 关于网络请求回调
@Override
public void onPostExecute(int statusCode, String action, String response, Object... o) {
    if (statusCode == HttpAsyncTask.CODE_SUCCESS) {
        // action是请求网络的完整地址
        // Get请求:action = ServerConfig.INTERVIEW_NOTICE_COUNT + params.toString()
        // Post请求:action = ServerConfig.INTERVIEW_NOTICE_COUNT
        if (action.contains(ServerConfig.INTERVIEW_NOTICE_COUNT)) {
            InterviewNotice interviewNotice = mApplication.getObject(response, InterviewNotice.class);
            TextView textView1 = (TextView) mActivity.findViewById(R.id.id_empty_line1);
            StringBuffer buffer = new StringBuffer();
            buffer.append("未读数:" + interviewNotice.getData().getUnreadCount() + "\r\n");
            buffer.append("返回码:" + interviewNotice.getResultCode() + "\r\n");
            buffer.append("返回消息:" + interviewNotice.getResultMsg());
            textView1.setText(buffer.toString());
        } else {
            // 其他请求成功返回 如果有多个请求 则应有else if()分支
            LogUtil.e(response);
        }
    }
}

异步加载图片

  • 加载网络图片资源
GlideUtil.display(bean.mImageString, holder.mImageView);
  • 加载本地图片资源
GlideUtil.displayLocal(file.getAbsolutePath(), holder.mImageView);

解析Json数据

  • 数据:json字符串
{"data":{"unreadCount":0},"resultCode":"000000","resultMsg":"成功!"}
  • Pojo:bean
package com.android.entity;

public class InterviewNotice extends BaseEntity {

    public NoticeCount data;
    public String resultCode;
    public String resultMsg;

    public NoticeCount getData() {
        return data;
    }
    public void setData(NoticeCount data) {
        this.data = data;
    }

    public String getResultCode() {
        return resultCode;
    }

    public void setResultCode(String resultCode) {
        this.resultCode = resultCode;
    }

    public String getResultMsg() {
        return resultMsg;
    }

    public void setResultMsg(String resultMsg) {
        this.resultMsg = resultMsg;
    }

    public static class NoticeCount {

        public int unreadCount;

        public int getUnreadCount() {
            return unreadCount;
        }

        public void setUnreadCount(int unreadCount) {
            this.unreadCount = unreadCount;
        }
    }
}
  • 解析方法(使用JessieApplication提供getObject()方法)
InterviewNotice interviewNotice = mApplication.getObject(response, InterviewNotice.class);

PullToRefreshListView

  • Xml文件中声明
<com.android.refresh.PullToRefreshListView
        android:id="@+id/id_empty_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
  • 代码中使用
mPullableView = (PullToRefreshListView) findViewById(R.id.id_empty_list);
mPullableView.setMode(PullToRefreshBase.Mode.BOTH);
mPullableView.setOnRefreshListener(new RefreshListenerImpl());
mAdapter = new PullableListViewAdapter(mContext);
mPullableView.setAdapter(mAdapter);
  • RefreshListenerImpl类
public class RefreshListenerImpl implements PullToRefreshBase.OnRefreshListener2<ListView> {

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                // 处理下拉刷新逻辑
                ToastUtil.show(mContext, "下拉刷新");
                mPullableView.onRefreshComplete();
            }
        }, 3000);
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                // 处理上拉加载逻辑
                ToastUtil.show(mContext, "上拉加载");
                mPullableView.onRefreshComplete();
            }
        }, 3000);
    }
}

代码中也有相应的示例,有兴趣的朋友可以下载试试,点击这里下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值