赞
踩
最近项目一期开发完成,有一点空闲时间,经过一段时间的准备,参考常见的网络应用并对其进行一定的研究,再结合自己的现有的一点点经验,封装了一个android应用开发”框架”,说是”框架”简直是夸下了海口,因为自己的那点小九九不足做出如此高大上的东西来,姑且叫”框架”吧。
“框架”概述
发起网络请求
mHttpAsyncTask.getMethod(ServerConfig.INTERVIEW_NOTICE_COUNT, this);
- 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);
- 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数据
{"data":{"unreadCount":0},"resultCode":"000000","resultMsg":"成功!"}
- 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;
- }
- }
- }
InterviewNotice interviewNotice = mApplication.getObject(response, InterviewNotice.class);
PullToRefreshListView
- <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);
- 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);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。