Android uses the Get and post methods of the HTTP protocol!!!

Source: Internet
Author: User

Just a few days ago, I studied the Get and post requests for the HTTP protocol inside Android:

Android has httpclient can be used to access the interface of the Web page, the following to introduce the use of HttpClient interface,

1.GET:

/** * GET Request * @param path Web path * @param params parameter * @param values of value parameter * @return JSON */Public static string Doget (String path,string[] params,string[] values) {try {HttpClient HttpClient = new Def            Aulthttpclient ();//Create httpclient object String url = urlparamutil.praisegetparams (path, params, values);//Combination page parameters HttpGet httpget = new HttpGet (URL);//Create Get object HttpResponse HttpResponse = Httpclient.execute (httpget);//Open Access, using HttpResponse to collect the returned data//If the connection is successful, read the data if (Httpresponse.getstatusline (). Getstatuscode () = = Httpstatus .        SC_OK) {return ReadData (httpresponse.getentity (). getcontent ());//through HttpResponse to obtain the corresponding output stream}        } catch (Clientprotocolexception e) {e.printstacktrace ();        } catch (IOException e) {e.printstacktrace ();    } return null; /** * Parse URL * @param path PATH * @param params parameter * @param values * @retuRN URL */public static string Praisegetparams (String path,string[] params,string[] values) {string url = ""        ; If params and values are empty, return to path if (params==null| |        values==null) {url = path; }//If the parameters and values differ in size, throw an exception else if (params.length! = values.length) {try {throw new Exce            Ption ("parameter exception");            } catch (Exception e) {e.printstacktrace ();            }}//normal parsing condition else{url = path + "?";            for (int i=0;i<params.length;i++) {URL + = (Params[i] + "=" + Values[i] + "&");        }//Intercept the last character "&" url = url.substring (0, Url.length ()-1);    } return URL;        /** * Read data through byte output stream * @param is * @return JSON */public static String ReadData (InputStream is) { BufferedReader br = new BufferedReader (new InputStreamReader);//convert byte output stream to character output stream StringBuffer sb = new String BuffeR ();        String line = "";            try {line = Br.readline ();                Loop reading data while (line! = null) {Sb.append (line + "\ n");            line = Br.readline ();        }} catch (IOException e) {e.printstacktrace ();    } return sb.tostring (); }

2.POST

/** * POST Request * @param path Web path * @param params parameter * @param values of value parameter * @return JSON */publi C Static String DoPost (String path,string[] params,string[] values) {try {HttpClient HttpClient = new D            Efaulthttpclient (); HttpPost HttpPost = new HttpPost (path);            Create post object//create parameter list<namevaluepair> pairs = Urlparamutil.praisepostparams (params, values);            httpentity entity = new Urlencodedformentity (pairs, "UTF-8");            Httppost.setentity (entity);            HttpResponse HttpResponse = Httpclient.execute (HttpPost); The request successfully starts reading the data if (Httpresponse.getstatusline (). Getstatuscode () = = HTTPSTATUS.SC_OK) {return readd            ATA (Httpresponse.getentity (). getcontent ());        }} catch (Unsupportedencodingexception e) {e.printstacktrace ();        } catch (Clientprotocolexception e) {e.printstacktrace ();   } catch (IOException e) {         E.printstacktrace ();    } return null; }/** * Parse post parameters * @return Pairs */public static list<namevaluepair> Praisepostparams (string[] pa        Rams,string[] values) {list<namevaluepair> pairs = new arraylist<namevaluepair> ();        Namevaluepair pair = null;            Iterate array creation parameters for (int i=0;i<params.length;i++) {pair = new Basicnamevaluepair (Params[i], values[i]);        Pairs.add (pair);    } return pairs; }


Note: These operations must be run in the thread


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android uses the Get and post methods of the HTTP protocol!!!

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.