/*** 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