极客匿名项目APP学习
来源:互联网 发布:黑莓手机下载软件 编辑:程序博客网 时间:2024/06/16 02:33
极客网,极客匿名项目APP学习学习总结:
该项目非常简单学到内容及运用到技术:
1.全部根据文档要求,规范化开发项目,一步一步的实现功能效果。
2.MD5工具使用
3.URLConnection进行网络连接,并进行封装。
4.AsyncTask异步任务的使用
5.获取联系人,并进行JSON转化。
6.缓存的使用
7.adapter的使用
项目Client和Server及文档下载地址:http://download.csdn.net/detail/itjavawfc/8553795
一)缓存使用:
public static final String KEY_TOKEN="token";//缓存tokenpublic static String getCachedToken(Context context){return context.getSharedPreferences(APP_ID, Context.MODE_PRIVATE).getString(KEY_TOKEN, null);}public static void cacheToken(Context context,String token){Editor e=context.getSharedPreferences(APP_ID, Context.MODE_PRIVATE).edit();e.putString(KEY_TOKEN, token);e.commit();}//缓存电话号码public static String getCachedPhoneNum(Context context){return context.getSharedPreferences(APP_ID, Context.MODE_PRIVATE).getString(KEY_PHONE_NUM, null);}public static void cachePhoneNum(Context context,String phoneNum){Editor e=context.getSharedPreferences(APP_ID, Context.MODE_PRIVATE).edit();e.putString(KEY_TOKEN, phoneNum);e.commit();}
二)AsyncTask异步任务的使用和NetConnection封装的URLConnection
public class NetConnection {public NetConnection(final String url,final HttpMethod method,final SuccessCallback successCallback,final FailCallback failCallback,final String ... kvs){new AsyncTask<Void, Void, String>() {@Overrideprotected String doInBackground(Void... params) {StringBuffer paramsStr=new StringBuffer();for(int i=0;i<kvs.length;i+=2){paramsStr.append(kvs[i]).append("=").append(kvs[i+1]).append("&");}paramsStr.substring(0, paramsStr.length()-2);try {URLConnection uc; //两种不同的方式来上传数据switch(method){case POST:uc=new URL(url).openConnection();uc.setDoOutput(true);BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(uc.getOutputStream(),Config.CHARSET));bw.write(paramsStr.toString());bw.flush();break;default:uc=new URL(url+"?"+paramsStr.toString()).openConnection(); break;}System.out.println("Request url:"+uc.getURL());System.out.println("Request data:"+paramsStr);//读取数据BufferedReader br=new BufferedReader(new InputStreamReader(uc.getInputStream(), Config.CHARSET));String line=null;StringBuffer result=new StringBuffer();while((line=br.readLine())!=null){result.append(line);}System.out.println("Result:"+result.toString());return result.toString();} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}@Overrideprotected void onPostExecute(String result) {super.onPostExecute(result);if(result!=null){if(successCallback!=null){successCallback.onSuccess(result);}}else{if(failCallback!=null){failCallback.onFail();}}}}.execute();}public static interface SuccessCallback{void onSuccess(String result);}public static interface FailCallback{void onFail();}}
利用封装类,借助它来从网络上获取数据:
public class UploadContacts {public UploadContacts(String phone_md5,String token,String contacts,final SuccessCallback successCallback,final FailCallback failCallback){new NetConnection(Config.SERVER_URL, HttpMethod.POST, new NetConnection.SuccessCallback() {@Overridepublic void onSuccess(String result) {try {JSONObject obj=new JSONObject(result); switch(obj.getInt(Config.KEY_STATUS)){ case Config.RESULT_STATUS_SUCCESS:if(successCallback!=null){successCallback.onSuccess();} break; case Config.RESULT_STATUS_INVALID_TOKEN: if(failCallback!=null){failCallback.onFail(Config.RESULT_STATUS_INVALID_TOKEN);} break; default: if(failCallback!=null){failCallback.onFail(Config.RESULT_STATUS_FAIL);} break; }} catch (JSONException e) {e.printStackTrace();if(failCallback!=null){failCallback.onFail(Config.RESULT_STATUS_FAIL);}}}}, new NetConnection.FailCallback() {@Overridepublic void onFail() {if(failCallback!=null){failCallback.onFail(Config.RESULT_STATUS_FAIL);}}}, Config.KEY_ACTION,Config.ACTION_UPLOAD_CONTACTS,Config.KEY_PHONE_MD5,phone_md5,Config.KEY_TOKEN,token,Config.KEY_CONTACTS,contacts);}public static interface SuccessCallback{void onSuccess();}public static interface FailCallback{void onFail(int errorCode);}}
三)读取联系人【一般项目都有的一个小功能】:
public class MyContacts {public static String getContactsJSONString(Context context){Cursor c=context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);String phoneNum;JSONArray jsonArr=new JSONArray();JSONObject jsonObj;while(c.moveToNext()){phoneNum=c.getString(c.getColumnIndex(Phone.NUMBER));if(phoneNum.charAt(0)=='+'&&phoneNum.charAt(1)=='8'&&phoneNum.charAt(2)=='6'){}jsonObj=new JSONObject();try {jsonObj.put(Config.KEY_PHONE_MD5, MD5Tool.md5(phoneNum));} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}jsonArr.put(jsonObj);}System.out.println(jsonArr.toString());return jsonArr.toString();}}
四)Adapter的创建:里面有添加和删除数据的方法,还有获得Context方法,非常有用。
public class AtyMessageCommentListAdapter extends BaseAdapter {private Context context;public Context getContext() {return context;}public AtyMessageCommentListAdapter(Context context) {this.context=context;}private List<Comment> comments=new ArrayList<Comment>();@Overridepublic int getCount() {return comments.size();}@Overridepublic Object getItem(int position) {return comments.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {if(convertView==null){convertView=LayoutInflater.from(getContext()).inflate(R.layout.aty_comments_list_cell, null);convertView.setTag(new ListCell((TextView)convertView.findViewById(R.id.tvCellLabel)));}ListCell lc=(ListCell) convertView.getTag();Comment comment=(Comment) getItem(position);lc.getTvCellLabel().setText(comment.getContent());return convertView;}public void addAll(List<Comment> data){comments.addAll(data);notifyDataSetChanged();}public void clear(){comments.clear();notifyDataSetChanged();}private static class ListCell{private TextView tvCellLabel;public ListCell(TextView tvCellLabel){this.tvCellLabel=tvCellLabel;}public TextView getTvCellLabel() {return tvCellLabel;}}}
0 0
- 极客匿名项目APP学习
- 匿名App项目小总结
- 面具-匿名社交APP
- 匿名---项目总结
- 安卓 仿学习app项目
- Android开源项目之电脑极客app
- 匿名内部类学习
- 匿名类的学习
- c#学习笔记-匿名
- python学习-匿名函数
- js 匿名函数学习
- 【C#学习】匿名方法
- 匿名项目笔记第一篇
- 《匿名》第五课----项目架构
- 匿名项目连载(一)--->项目概要
- Android开源项目学习 | QKSMS短信app
- Java的匿名类学习
- JavaScript学习之 匿名函数
- 关于系统自带的UITableView左滑删除,注意一点
- 正则总结:JavaScript中的正则表达式
- 标准c内存函数
- Android ListView 去除边缘阴影、选中色、拖动、底部上拉、顶部下拉背景色等
- 其他标准c函数
- 极客匿名项目APP学习
- windows系统下oracle数据库定时自动备份
- C++ I/O
- 环境变量PATH有什么用?看过这篇你就懂了
- 一个Google的面试题
- Xcode6.2 使用symbolicatecrash分析crash文件
- java向上转型与向下转型
- 编程如编心
- 学习cloudsim-2.1.1以及实现动态双阈值虚拟机迁移策略(1)