Android Server Action Class

Source: Internet
Author: User

Simple and convenient

/** * @author think * Send HTTP requests synchronously */public class Apachehttpclient {/** * @return * */Public Stri    ng HttpGet (String uri) {string response=null;//response HttpClient httpclient=new defaulthttpclient ();    Create HttpGet Object HttpGet httpget=new httpget (URI);    HttpResponse HttpResponse; try {//Use the Execute method to send an HTTP GET request and return the HttpResponse object Httpresponse=httpclient.execute (httpget); int statusCode = Httpresponse.getstatusline (). Getstatuscode ();//Return code, if (STATUSCODE==HTTPSTATUS.SC_OK) {//Get return result response= Entityutils.tostring (Httpresponse.getentity ());} else {response = "return code:" +statuscode;}}    catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();}  SYSTEM.OUT.PRINTLN (response);           return response;}    /** * Send Request as Post * @param URL request address * @param params parameter, post must be stored with namevaluepair[] array parameters * @return * @throws Exception * *        public string HttpPost (string uri,list<namevaluepair> params) throws exception{string Response=null;HttpClient httpclient=new defaulthttpclient ();    HttpPost httppost=new HttpPost (URI); try {//Set HttpPost request parameter httppost.setentity (new Urlencodedformentity (params,http.        Utf_8));        Use the Execute method to send an HTTP POST request and return the HttpResponse object HttpResponse httpresponse=httpclient.execute (HttpPost); int statusCode = Httpresponse.getstatusline (). Getstatuscode ();//Return code, if (STATUSCODE==HTTPSTATUS.SC_OK) {response=e Ntityutils.tostring (Httpresponse.getentity ());        System.out.println ("______________" +response);        else {response = "return code:" +statuscode; System.out.println ("______________" +response);}    catch (Exception e) {e.printstacktrace ();} return response;}}


Android Server Action Class

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.