Android HTTP-based network communication ---- HttpClient

Source: Internet
Author: User

HttpClient is also an Http network communication method, which is also divided into get and post methods. Let's take a look.

I. HttpClient mode ---- get

Before that, we used the Server File in the previous blog to return the information to the client based on the input parameters. The specific file content is as follows:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><%    String a = request.getParameter("a");    String b = request.getParameter("b");    if(a!=null&&b!=null){    out.println(a + b);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }else {        out.print("error");    }%>

Procedure:

1) define a String type String and assign the server address to it. Because the get method is used here, the parameter transmission is directly displayed on the URL path. Pay attention to the parameter transmission method. The details are as follows:

String strURL = "http://222.27.166.10:8080/MyServer/youcan.jsp?a=hello&b=word";

2) create an HttpGet data request object and instantiate the request object based on the string of the Flag server address.

httpGet = new HttpGet(strURL);

3) create a client object to send the preceding request. Because it is an abstract class, it needs to be instantiated through its subclass.

httpClient = new DefaultHttpClient();

4) Create the HttpResponse object for response. It is used to receive the response object returned by the server after the client object executes the request.

httpResponse = httpClient.execute(httpGet);

5) Create the HttpEntity object for receiving information and obtain the response information through the getEntity () method of the HttpResponse object.

httpEntity = httpResponse.getEntity();

6) Create an input stream object, obtain the returned information through the getContent method of the HttpEntity object, and read and output the information on the TextView control through the stream object.

in =httpEntity.getContent();                                                                                                                                                                                                                                                                                                                                                                 BufferedReader br = new BufferedReader(new InputStreamReader(in));                                                                                                                                                                                                                                                                                                                                                                 String line = null;            StringBuffer sb = new StringBuffer();            while ((line = br.readLine())!=null) {                sb.append(line);            }//          System.out.println(sb.toString());            tv.setText(sb.toString());

7) Note: Add network permissions.

<uses-permission android:name="android.permission.INTERNET"/>

Result: The following two strings are spliced on the frequency screen of the simulator.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/102125E27-0.jpg "title =" Unnamed .bmp "/>

Ii. HttpClient mode ---- Post

We also reference the previous Server File to return the concatenated string.

Specific operations:

1) create a data request object HttpPost. Here we pass in the flag server address string in the constructor method to instantiate the object. Note that parameters cannot be directly written in the strings, but data is carried to the server using the following methods.

httpPost = new HttpPost("http://222.27.166.10:8080/MyServer/youcan.jsp");

2) create a client object HttpClient

httpClient = new DefaultHttpClient();

3) create a List with the generic type BasicNameValuePair. The content of this generic type is stored as a key-value pair.

List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();        list.add(new BasicNameValuePair("a", "hello"));        list.add(new BasicNameValuePair("b","word"));

4) create a data storage object UrlEncodedFormEntity and instantiate the UrlEncodedFormEntity object with the data stored in List.

urlEncodedFormEntity = new UrlEncodedFormEntity(list);

5) Add the passed parameter value to the data request object

httpPost.setEntity(urlEncodedFormEntity);

6) instantiate the object that receives the response

httpResponse  = httpClient.execute(httpPost);

7) Create the HttpEntity object for receiving information and obtain the response information through the getEntity () method of the HttpResponse object

httpEntity = httpResponse.getEntity();

8) Create an input stream object, obtain the returned information through the getContent method of the HttpEntity object, and read and output the information on the TextView control through the stream object.

in = httpEntity.getContent();            BufferedReader br = new BufferedReader(new InputStreamReader(in));            String line=null;            StringBuffer sb=new StringBuffer();            while ((line = br.readLine())!=null) {                sb.append(line);                                                                                      }            tv.setText(sb.toString());

9) Note: you must add network communication permissions.

<uses-permission android:name="android.permission.INTERNET"/>

Result: The following two strings are spliced on the frequency screen of the simulator. The result is the same as above)


Network Communication in Android is very useful. For example, we can obtain the Json string in the network through network communication and parse it for our use. We'll see you tomorrow 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1021253216-1.gif "/>


This article is from the "Schindler" blog, please be sure to keep this source http://cinderella7.blog.51cto.com/7607653/1290171

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.