1. Backgroundin the development of Android, the network request is necessary, in this, encapsulated into a class, convenient to call;
2. Notehere HttpURLConnection (not httpsurlconnection) get requests and HttpClient get and POST requests are implemented! this encapsulates only the operation of the data, not including the picture request and upload!
3.HttpUrlConnection implementations after this request is successful, I need to use IO stream to read!
public static String GetData () throws exception{try {//httppath.getgamespath (): Network request path URL url=new url (httppath.getgames Path ()); HttpURLConnection conn= (httpurlconnection) url.openconnection (); conn.setconnecttimeout (5000); Conn.setrequestmethod ("GET"), if (Conn.getresponsecode () ==200) { inputstream ips=conn.getinputstream (); Byte[] Data=read (IPS); String Str=new string (data); System.out.println (str); return str;} else{ return "Network error: Conn.getresponsecode () =" +conn.getresponsecode ();}} catch (Malformedurlexception e) {//TODO Auto-generated catch Blockreturn "Httpservice.getgamesdata () an exception occurred! "+e.getmessage ();}} /* * Read data from stream * */public static byte[] Read (InputStream instream) throws Exception {Bytearrayoutputstream Outstream=new Bytearrayoutputstream (); byte[] Buffer=new byte[1024];int len=0;while ((len=instream.read (buffer))!=-1 ) {outstream.write (Buffer,0,len);} Instream.close (); return Outstream.tobytearray ();}
4.Httpclient, HttpPost, eneityutils implement POST request Operationnote here is to pass the parameter when the need is list<basicnamevaluepair> , parameter formatting needs is the urlencodeformentity Method!
/** * HttpClient Post submit data * @param parameters * @return * @throws clientprotocolexception * @throws IOException */public s Tatic String topostdata (list<basicnamevaluepair> parameters) throws Clientprotocolexception, ioexception{ String str= "Get Failed";//Initialize httpclient client=new defaulthttpclient ();//httppath.getgamespath (): Network request path HttpPost Httppost=new HttpPost (Httppath.getgamespath ());//Set Parameters urlencodedformentity params=new urlencodedformentity ( Parameters, "UTF-8"); Httppost.setentity (params); Execution Request HttpResponse response= Client.execute (httppost); Gets the return value if (Response.getstatusline (). Getstatuscode () ==200) {//Request succeeded Httpentity entity=response.getentity (); str= Entityutils.tostring (Entity, "UTF-8");} return str;}
5.Httpclient, HttpGet, eneityutils implement GET request Operation
/** * httpclient Get Data * @return */public static string Togetdata () {string str= ' get data failed '; HttpClient client=new defaulthttpclient ();////httppath.getgamespath (): Network request path HttpGet get=new HttpGet ( Httppath.getgamespath ()); try { HttpResponse httpresponse=client.execute (get); if (Httpresponse.getstatusline (). Getstatuscode () ==200) { str=entityutils.tostring (httpresponse.getentity (), " UTF-8 "); } return str; } catch (Clientprotocolexception e) {//TODO auto-generated catch Blockreturn "Togetdata exception:" +e.getmessage ();} catch (Ioexc Eption e) {//TODO auto-generated catch Blockreturn "Togetdata exception:" +e.getmessage ();}}
6. ContrastBy contrast HttpClient is more convenient than httpurlconnection, but still look at the habit!
7. Tools Downloadhttp://download.csdn.net/detail/lablenet/9005725
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android-encapsulating Post and get network requests