Android development-Http operations (1)

Source: Internet
Author: User

What is HTTP? 1. hypertext Transfer Protocol is the most widely used network protocol on the Internet. HTTP is a standard for client and server requests and responses. The client is an end user and the server is a website. HTTP is the application layer communication protocol between the client browser or other applications and the Web server. How HTTP Works 1. establish a connection between the client and the server. after the connection is established, the client wants the server to send a request. 3. after receiving the request, the server sends the response information to the client. client and server disconnected Note: The fourth article here requires attention, that is, when the user sees the following interface, the client has been disconnected from the server. The following is a simple example of how to connect to the server and obtain data from the server. It is running: when a user clicks the button, the user sends a request to the server, the returned data is displayed in the textview below. The specific implementation code is as follows: [java] p <span style = "font-size: 18px; "> ublic class MainActivity extends Activity {private Button button; private TextView textView; private HttpResponse httpResponse = null; private HttpEntity httpEntity = null; @ Override protected void onCreate (Bundle savedInstanceState. onCreate (savedInstanceSta Te); setContentView (R. layout. activity_main); button = (Button) findViewById (R. id. button); textView = (TextView) findViewById (R. id. textview); button. setOnClickListener (new OnClickListener () {@ Override public void onClick (View arg0) {// TODO Auto-generated method stub // generate a request object HttpGet httpGet = new HttpGet ("http://www.baidu.com "); // generate an Http client object HttpClient httpClient = new DefaultHttpClient (); // use the Http client The client sends the request object InputStream inputStream = null; try {httpResponse=httpClient.exe cute (httpGet); // read the returned data after receiving the response from the server. httpEntity = httpResponse. getEntity (); inputStream = httpEntity. getContent (); // stream File Read BufferedReader reader = new BufferedReader (new InputStreamReader (inputStream); String resultString = ""; String lineString = ""; while (lineString = reader. readLine ())! = Null) {resultString = resultString + lineString;} textView. setText (resultString);} catch (ClientProtocolException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();} finally {try {inputStream. close () ;}catch (Exception e2) {// TODO: handle exception e2.printStackTrace () ;}}}) ;}@ Override public boolean onCreateOptionsMenu (Menu menu Menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. activity_main, menu); return true ;}</span> Note: Due to network connection, you must declare the network permission in AndroidManifest: [html] <uses-permission android: name = "android. permission. INTERNET "/>

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.