Example of a POST request and a GET request sent by Android

Source: Internet
Author: User
Package COM. example. demorequest; import Java. io. bufferedreader; import Java. io. inputstream; import Java. io. inputstreamreader; import Java. util. arraylist; import Java. util. list; import Org. apache. HTTP. httpentity; import Org. apache. HTTP. httpresponse; import Org. apache. HTTP. namevaluepair; import Org. apache. HTTP. client. httpclient; import Org. apache. HTTP. client. entity. urlencodedformentity; import Org. apache. HTTP. c Lient. methods. httpget; import Org. apache. HTTP. client. methods. httppost; import Org. apache. HTTP. impl. client. defaulthttpclient; import Org. apache. HTTP. message. basicnamevaluepair; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. edittext; public class mainactivity extends activity {priv Ate button getbutton = NULL; private button postbutton = NULL; private edittext strview = NULL; private string baseurl = "http://www.baidu.com/s? "; Private httpresponse = NULL; // response object private httpentity = NULL; // The message object inputstream that retrieves the response content = NULL; // input stream object/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); strview = (edittext) findviewbyid (R. id. strview); getbutton = (button) find Viewbyid (R. id. getbutton); postbutton = (button) findviewbyid (R. id. postbutton); // get method sends the request getbutton. setonclicklistener (New onclicklistener () {public void onclick (view arg0) {string STR = strview. gettext (). tostring (); string url = baseurl + "? WD = "+ STR; // generate a request object httpget = new httpget (URL); // generate an HTTP client object httpclient = new defaulthttpclient (); // send the request try {httpresponse = httpclient.exe cute (httpget); // receive the response httpentity = httpresponse. getentity (); // retrieve the response // inputstream of the message stream received by the client = httpentity. getcontent (); bufferedreader reader = new bufferedreader (New inputstreamreader (inputstream); string result = ""; string line = ""; While (line = reader. Readline ())! = NULL) {result = Result + line;} system. out. println (result);} catch (exception e) {// todo auto-generated Catch Block E. printstacktrace ();} finally {// you must close the input stream try {inputstream. close ();} catch (exception e) {e. printstacktrace () ;}}}); // the POST method sends the request postbutton. setonclicklistener (New onclicklistener () {public void onclick (view arg0) {// todo auto-generated method stub string STR = strview. ge Ttext (). tostring (); // parameter namevaluepair = new basicnamevaluepair ("content", STR); // key-Value Pair // then put the key-value pair in the list (similar to forming an array) // list is an interface, while listarray is a class. Listarray inherits and implements the list. Therefore, the list cannot be constructed, but a reference can be created for the list as above, and listarray can be constructed. // List = new arraylist (); this statement creates an arraylist object and traces it back to list. At this time, it is a list object // and arraylist list = new arraylist (); when an object is created, all attributes of arraylist are retained. // Why do I usually use list = new arraylist () instead of arraylist alist = new arraylist? The problem is that list has multiple implementation classes, such as struct list or vector. Now you are using arraylist. Which day do you need to change to another implementation class ?, In this case, you only need to change this line: List list = new sort list (); Other code that uses list does not need to be modified at all. List <namevaluepair> namevaluepairs = new arraylist <namevaluepair> (); namevaluepairs. add (namevaluepair); // put key-value pairs in the list try {httpentity requesthttpentity = new urlencodedformentity (namevaluepairs ); // encode the parameters // generate a POST request object httppost = new httppost (baseurl); httppost. setentity (requesthttpentity); // generate an HTTP client object httpclient = new defaulthttpclient (); // send a request try {httpresponse = httpc Lient.exe cute (httppost); // receives the response httpentity = httpresponse. getentity (); // retrieve the response // inputstream of the message stream received by the client = httpentity. getcontent (); bufferedreader reader = new bufferedreader (New inputstreamreader (inputstream); string result = ""; string line = ""; while (line = reader. readline ())! = NULL) {result = Result + line;} system. out. println (result);} catch (exception e) {// todo auto-generated Catch Block E. printstacktrace ();} finally {// you must close the input stream try {inputstream. close ();} catch (exception e) {e. printstacktrace () ;}} catch (exception e) {// todo auto-generated Catch Block E. printstacktrace ();}}});}}

<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns: Tools = "http://schemas.android.com/tools" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: orientation = "vertical"> <edittext Android: Id = "@ + ID/strview" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"/> <button Android: id = "@ + ID/getbutton" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "send a request using the get method"/> <button Android: id = "@ + ID/postbutton" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "send requests using post"/> </linearlayout>
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.