HttpClient in Android practice

Source: Internet
Author: User
Tags call back

Recently, I am reading Android development. HttpClient is used to submit or obtain server-side data. However, the API provided by Android is still a little unavailable, so I made a package according to my own needs, as shown below:

 

The HttpConnectionUtil class is a tool class that provides synchronous and asynchronous methods, and currently supports http Get and Post methods.

Import java. io. bufferedReader; <br/> import java. io. IOException; <br/> import java. io. inputStreamReader; <br/> import java. io. unsupportedEncodingException; <br/> import java.net. URLEncoder; <br/> import java. util. arrayList; <br/> import java. util. list; <br/> import java. util. map; <br/> import org. apache. http. httpResponse; <br/> import org. apache. http. httpStatus; <br/> import org. apache. http. nameValuePair; <br /> Import org. apache. http. client. clientProtocolException; <br/> import org. apache. http. client. httpClient; <br/> import org. apache. http. client. entity. urlEncodedFormEntity; <br/> import org. apache. http. client. methods. httpGet; <br/> import org. apache. http. client. methods. httpPost; <br/> import org. apache. http. client. methods. httpUriRequest; <br/> import org. apache. http. impl. client. defaultHttpClient; <br/> imp Ort org. apache. http. message. basicNameValuePair; <br/> import android. OS. handler; <br/> import android. util. log; <br/> public class HttpConnectionUtil {<br/> public static enum HttpMethod {GET, POST} </p> <p> public void asyncConnect (final String url, final HttpMethod method, final HttpConnectionCallback callback) {<br/> asyncConnect (url, null, method, callback ); <br/>}</p> <p> public void syncConnect (fina L String url, final HttpMethod method, final HttpConnectionCallback callback) {<br/> syncConnect (url, null, method, callback ); <br/>}</p> <p> public void asyncConnect (final String url, final Map <String, String> params, <br/> final HttpMethod method, final HttpConnectionCallback callback) {<br/> Handler handler = new Handler (); <br/> Runnable runnable = new Runnable () {<br/> public void run () {<br/> sy NcConnect (url, params, method, callback); <br/>}< br/>}; <br/> handler. post (runnable); <br/>}</p> <p> public void syncConnect (final String url, final Map <String, String> params, <br/> final HttpMethod method, final HttpConnectionCallback callback) {<br/> String json = null; <br/> BufferedReader reader = null; </p> <p> try {<br/> HttpClient client = new DefaultHttpClient (); <br/> HttpUriRequest request = ge TRequest (url, params, method); <br/> HttpResponse response = client.exe cute (request); <br/> if (response. getStatusLine (). getStatusCode () = HttpStatus. SC _ OK) {<br/> reader = new BufferedReader (new InputStreamReader (response. getEntity (). getContent (); <br/> StringBuilder sb = new StringBuilder (); <br/> for (String s = reader. readLine (); s! = Null; s = reader. readLine () {<br/> sb. append (s); <br/>}< br/> json = sb. toString (); <br/>}< br/>} catch (ClientProtocolException e) {<br/> Log. e ("HttpConnectionUtil", e. getMessage (), e); <br/>}catch (IOException e) {<br/> Log. e ("HttpConnectionUtil", e. getMessage (), e); <br/>}finally {<br/> try {<br/> if (reader! = Null) {<br/> reader. close (); <br/>}< br/>} catch (IOException e) {<br/> // ignore me <br/>}< br/> callback.exe cute (json ); <br/>}</p> <p> private HttpUriRequest getRequest (String url, Map <String, String> params, HttpMethod) {<br/> if (method. equals (HttpMethod. POST) {<br/> List <NameValuePair> listParams = new ArrayList <NameValuePair> (); <br/> if (params! = Null) {<br/> for (String name: params. keySet () {<br/> listParams. add (new BasicNameValuePair (name, params. get (name); <br/>}< br/> try {<br/> UrlEncodedFormEntity entity = new UrlEncodedFormEntity (listParams ); <br/> HttpPost request = new HttpPost (url); <br/> request. setEntity (entity); <br/> return request; <br/>} catch (UnsupportedEncodingException e) {<br/> // shocould not come here, ignore Me. <br/> throw new java. lang. runtimeException (e. getMessage (), e); <br/>}< br/>} else {<br/> if (url. indexOf ("? ") <0) {<br/> url + = "? "; <Br/>}< br/> if (params! = Null) {<br/> for (String name: params. keySet () {<br/> url + = "&" + name + "=" + URLEncoder. encode (params. get (name); <br/>}< br/> HttpGet request = new HttpGet (url); <br/> return request; <br/>}< br/> 

 

 

The HttpConnectionCallback class is a callback class used to process the logic after the request is completed.

Public interface HttpConnectionCallback {<br/>/** <br/> * Call back method will be execute after the http request return. <br/> * @ param response the response of http request. <br/> * The value will be null if any error occur. <br/> */<br/> void execute (String response); <br/>}< br/> 

 

 

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.