標籤:極客匿名項目app 社交android 社交app
極客網,極客匿名項目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;}}}
極客匿名項目APP學習