Two common types of Android network operations

Source: Internet
Author: User
Tags getmessage http post tostring

The Android SDK integrates the Apache httpclient module. Note that the Apache HttpClient module here is httpclient 4.0 (org.apache.http.*) rather than the common Jakarta Commons httpclient 3.x (org.apache.commons.httpclient.*).

HttpClient commonly used in the HttpGet and HttpPost these two classes, respectively corresponding to the Get and post methods.

Whether you are using HttpGet or using HttpPost, you must access the HTTP resources by following 3 steps.

1. Create a HttpGet or HttpPost object that will pass the URL of the request to the HttpGet or HttpPost object by constructing the method.

2. Use the Execute method of the Defaulthttpclient class to send an HTTP GET or HTTP POST request and return the HttpResponse object.

3. Return the response information through the GetEntity method of the HttpResponse interface and handle it accordingly.

If you submit an HTTP POST request using the HttpPost method, you need to set the request parameters using the Setentity method of the HttpPost class. Parameters must be stored with the namevaluepair[] array.

Some examples are given below:

Get Way:

//HttpGet mode request public static void Requestbyhttpget () throws Exception {String path = ' https://reg.163.c    
    Om/logins.jsp?id=helloworld&pwd=android ";    
    New HttpGet object HttpGet httpget = new HttpGet (path);    
    Gets the HttpClient object httpclient httpclient = new Defaulthttpclient ();    
    Get HttpResponse instance HttpResponse Httpresp = Httpclient.execute (HttpGet); Judgment is enough to request success if (Httpresp.getstatusline (). Getstatuscode () = = http_200) {//Get returned data String    
        result = Entityutils.tostring (Httpresp.getentity (), "UTF-8");    
        LOG.I (Tag_httpget, "HttpGet the way the request succeeds, returns the data as follows:");    
    LOG.I (tag_httpget, result);    
    else {log.i (Tag_httpget, "HttpGet method request Failed"); }    
}
 public String Doget () {String uriapi = "Http://XXXXX?str=I+am+get+String";    
String result= "";    
HttpGet httprequst = new HttpGet (URI Uri);    
HttpGet httprequst = new HttpGet (String URI);    
        Creates a httpget or HttpPost object that passes the URL of the request to the HttpGet or HttpPost object by constructing the method.    
        
HttpGet httprequst = new HttpGet (URIAPI);    
        New Defaulthttpclient (). Execute (httpurirequst requst);    
            try {//Use the Execute method of the Defaulthttpclient class to send an HTTP GET request and return the HttpResponse object. HttpResponse HttpResponse = new Defaulthttpclient (). Execute (httprequst);//Where HttpGet is a subclass of Httpurirequst if (HT Tpresponse.getstatusline () getstatuscode () = httpentity httpentity = Httprespo    
                Nse.getentity (); result = Entityutils.tostring (httpentity);//Remove answer string//generally delete extra characters result.replaceal L ("\ R", "");//Remove the "\ r" character in the return result, otherwise a small square is displayed after the result string      
            else Httprequst.abort (); catch (Clientprotocolexception e) {//TODO auto-generated catch block E.printstacktrace    
            ();    
        result = E.getmessage (). toString ();    
            catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();    
        result = E.getmessage (). toString ();    
    return result; }

Related Article

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.