Simple Example of HttpClient in android

Source: Internet
Author: User

 

Public boolean uploadHttpClient (String path, Map <String, String> params) throws IOException {

// 1. Put the parameter into a list set. Each parameter is a NameValuePair object.

List <NameValuePair> paramPair = new ArrayList <NameValuePair> ();

If (params! = Null &&! Params. isEmpty ()){

For (Map. Entry <String, String> entry: params. entrySet ()){

ParamPair. add (new BasicNameValuePair (entry. getKey (), entry. getValue ()));

}

}

// 2. Create a request object. The parameter set is used as the construction parameter.

UrlEncodedFormEntity entity = new UrlEncodedFormEntity (paramPair, "UTF-8 ");

// 3. Create a post request and put the object in

HttpPost post = new HttpPost (path );

Post. setEntity (entity );

// 4. Create a request client and execute the request to obtain the corresponding

DefaultHttpClient client = new DefaultHttpClient ();

HttpResponse res = client.exe cute (post );

If (res. getStatusLine (). getStatusCode () = 200 ){

Return true;

}

Return false;

}

SLL or Https is used in the project. It is convenient to use this open-source project when using cookies. If it is not used, it is more efficient to directly operate the http protocol.

From: com360 Column

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.