Geek Anonymous Project App learning

Source: Internet
Author: User

Geek Web, Geek Anonymous Project App Learning Summary:

The project is very simple to learn the content and apply to the technology:

1. All according to the requirements of the document, standardized development projects, step by step to achieve functional effects.

2.MD5 tool Use

3.URLConnection for network connectivity and encapsulation.

4.AsyncTask Use of asynchronous tasks

5. Get the contacts and convert them to JSON.

6. Use of the cache

Use of 7.adapter

Project client and server and documentation: http://download.csdn.net/detail/itjavawfc/8553795



A) Cache usage:

public static final String key_token= "TOKEN";//cache 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 ();} Cache phone number 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 ();}

II) The use of Asynctask asynchronous tasks and the urlconnection of netconnection encapsulation

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; Two different ways to upload a data 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);//Read Data 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 Au To-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 ();}}

Use the encapsulation class to get data from the network:

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);}}


III) Read the contact "a small feature of the General project":

public class MyContacts {public static String getcontactsjsonstring (context context) {Cursor c= Context.getcontentresolver (). query (Phone.content_uri, NULL, NULL, NULL, and NULL); String Phonenum; Jsonarray jsonarr=new Jsonarray (); Jsonobject Jsonobj;while (C.movetonext ()) {phonenum=c.getstring (C.getcolumnindex (Phone.number)); 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 ();}}


IV) Creation of adapter: There are methods for adding and deleting data, and it is useful to obtain the context method.

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;}}}



Geek Anonymous Project App learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.