"Turn" Pro Android Learning Note (68): HTTP Service (2): HTTP POST

Source: Internet
Author: User
Tags http 200 http post

Directory (?) [-]

    1. Find a test Site
    2. HTTP Post Small example

Last time you learned an HTTP GET request, learn about HTTP POST this time.

Find a test Site

Small example is good to write, but to find a test site is a bit of trouble, all of a sudden there is no way to start, consider whether the next Apache, get one yourself. Finally found a paper query site, the result form content is very long, and many (less words directly with get). Then a wireshare is carried out to grab the packet, as below, directly from the HTTP request message to see which parameters to pass. (PS: A bit more parameters, always better than a Web server)

HTTP Post Small example

The effect of HTTP POST and HTTP GET is very similar, unlike the Execute () parameter of the client is an HttpPost instance, not a HttpGet instance. Specific as follows:

BufferedReader in = null;
try{
"Step 1" Create an HttpClient object (or use an existing one)
HttpClient client = new Defaulthttpclient ();
//"Step 2" instantiates an HTTP GET or HTTP POST, this example is an HTTP post
HttpPost request = new HttpPost ("http://epub.cnki.net/kns/brief/default_result.aspx");

//"Step 3" to set the HTTP parameters,This example is based on the contents of the capture package, which is a manual work, after the full HTTP service notes, will provide a small example download.for HTTP Post, you need to pass the key-value pair information, as you can see from the above subcontracting, which is not as the request URI, but as the HTML Form URL encoded, For this we need the user element to store this information in the Namevaluepair format list and encapsulate it in the Urlencodedformentiry object. Added to the Request object by Setentity ().
list<namevaluepair> postparameters = new arraylist<namevaluepair> ();
Postparameters.add (New Basicnamevaluepair (
"Txt_1_sel", "ti$%=|"));
Postparameters.add (New Basicnamevaluepair ("Txt_1_value1", "Android"));
... ...
Postparameters.add (New Basicnamevaluepair ("UA", "1.11"));
urlencodedformentity formentity = new urlencodedformentity (postparameters);
Request.setentity (formentity);


//"Step 4" to execute HTTP call via HttpClient (HTTP request issued)
HttpResponse Response =client.execute (request);

"Step 5" handles the HTTP response, in this case, the contents of the entire response (the body of the HTTP 200 message) are in string.
in = new BufferedReader (
New InputStreamReader (
Response.getentity (). getcontent ());

StringBuffer buff = new StringBuffer ("");
String line = "";
String NL = System.getproperty ("Line.separator");
while (line = In.readline ()) = null) {
Buff.append (line + NL);
}
Showinfo (Buff.tostring ());
}catch (Exception e) {
E.printstacktrace ();
Showinfo (E.tostring ());
}finally{
if (in = null) {
try{
Showinfo ("= = Process In.colse () = =");
In.close ();
}catch (Exception e) {
E.printstacktrace ();
Showinfo (E.tostring ());
}
}
}

The example code covered in this blog post can be downloaded in the Pro Android Learning: Http Service Small example.

RELATED Links: My Android development related articles

"Turn" Pro Android Learning Note (68): HTTP Service (2): HTTP POST

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.