Use HTTP and httpclient for communication in Android

Source: Internet
Author: User

/*** Data requests using the Get method of HTTP*/    protected voidHttpGet () {/*** Make asynchronous requests*/        NewAsynctask<string, Void, void>() {@OverrideprotectedVoid doinbackground (String ... params) {System.err.println ("HttpGet Start"); //data processing is only possible in this method and cannot interact with UI                Try{URL URL=NewURL (params[0]); URLConnection Connection=url.openconnection (); InputStream is=Connection.getinputstream (); //data stream conversion using UTF-8, from byte flow to character streamInputStreamReader ISR =NewInputStreamReader (IS, "UTF-8"); BufferedReader BR=NewBufferedReader (ISR); String Line=NULL;  while(line = Br.readline ())! =NULL) {System.err.println (line); }                    //Turn off Data flowBr.close ();                    Isr.close ();                Is.close (); } Catch(malformedurlexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }                return NULL; }}.execute ("http://fanyi.youdao.com/openapi.do?keyfrom=***&key=***&type=data&doctype=json&version=1.1 &q=good "); }    /*** Data requests using the Post method of HTTP*/    protected voidHttpPost () {/*** Make asynchronous requests*/        NewAsynctask<string, Void, void>() {@OverrideprotectedVoid doinbackground (String ... params) {System.err.println ("HttpPost Start"); //data processing is only possible in this method and cannot interact with UI                Try{URL URL=NewURL (params[0]); HttpURLConnection Connection=(httpurlconnection) URL. OpenConnection (); //setting request methods and setting parameters//sets whether to output to the HttpURLConnection objectConnection.setdooutput (true); //Set Request ModeConnection.setrequestmethod ("POST"); //Setting ParametersOutputStream OS =Connection.getoutputstream (); OutputStreamWriter OSW=NewOutputStreamWriter (OS, "UTF-8")); BufferedWriter BW=NewBufferedWriter (OSW); Bw.write (params[1]);                    Bw.flush (); InputStream is=Connection.getinputstream (); //data stream conversion using UTF-8, from byte flow to character streamInputStreamReader ISR =NewInputStreamReader (IS, "UTF-8"); BufferedReader BR=NewBufferedReader (ISR); String Line=NULL;  while(line = Br.readline ())! =NULL) {System.err.println (line); }                    //Turn off Data flowBr.close ();                    Isr.close ();                Is.close (); } Catch(malformedurlexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }                return NULL; }}.execute ("Http://fanyi.youdao.com/openapi.do",                "Keyfrom=***&key=***&type=data&doctype=json&version=1.1&q=good"); }    PrivateHttpClient HttpClient; /*** Use httpclient for GET requests*/    protected voidHttpclientget () {NewAsynctask<string, Void, void>() {@OverrideprotectedVoid doinbackground (String ... params) {System.err.println ("Httpclientget Start"); String urlstring= Params[0]; HttpGet HttpGet=NewHttpGet (urlstring); Try {                    //Send RequestHttpResponse response =Httpclient.execute (HttpGet); //Get back contentString value =entityutils.tostring (Response.getentity ());                System.err.println (value); } Catch(clientprotocolexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }                return NULL; }}.execute ("http://fanyi.youdao.com/openapi.do?keyfrom=***&key=***&type=data&doctype=json&version=1.1 &q=good "); }    /*** Use httpclient for post requests*/    protected voidHttpclientpost () {NewAsynctask<string, Void, void>() {@OverrideprotectedVoid doinbackground (String ... params) {System.err.println ("Httpclientpost Start"); String urlstring= Params[0]; HttpPost HttpPost=NewHttpPost (urlstring); Try {                    //Setting Parametersstring[] Paramsarr = Params[1].split ("&"); intLen =paramsarr.length; List<BasicNameValuePair> list =NewArraylist<basicnamevaluepair>(len); Basicnamevaluepair pair=NULL;  for(inti = 0; i < Len; i++) {string[] Paramarr= Paramsarr[i].split ("="); Pair=NewBasicnamevaluepair (Paramarr[0], paramarr[1]);                    List.add (pair); } httppost.setentity (Newurlencodedformentity (list)); } Catch(unsupportedencodingexception E1) {e1.printstacktrace (); }                Try {                    //Send RequestHttpResponse response =Httpclient.execute (HttpPost); //Get back contentString value =entityutils.tostring (Response.getentity ());                System.err.println (value); } Catch(clientprotocolexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }                return NULL; }}.execute ("Http://fanyi.youdao.com/openapi.do",                "Keyfrom=***&key=***&type=data&doctype=json&version=1.1&q=good"); }

:

Use HTTP and httpclient for communication in Android

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.