Using the HTTP service:

1.
Apache HttpClinet
Http GET
Http POST

a. Create HttpClient
b. The initial HTTP GET method or POST method.
c. set the parameters of key pairs
d. implementation of the HTTP call
e. processing HTTP reply

HTTP GET example:

1
  public class TestHttpGetMethod (public void get () (BufferedReader in = null; try (HttpClient client = new DefaultHttpClient (); HttpGet request = new HttpGet (); request.setURI ( "http://w26.javaeye.com"); HttpResponse response = client.execute (request); in = new BufferedReader (new InputStreamReader (response.getEntity (). getContent ())); StringBuffer sb = new StringBuffer (""); String line = ""; String NL = System . getProperty ( "line.separator"); while ((line = in.readLine ())! = null) (sb.append (line   NL);) in.close (); String page = sb.toString () ; Log.i (TAG, page);) catch (Exception e) (Log.e (TAG, e.toString ())) finally (if (in! = null) (try (in.close ();) catch (IOException ioe) (Log.e (TAG, ioe.toString ());)))))

With parameters of HTTP GET:

1
  HttpGet request = new HttpGet ( "http://www.baidu.com/s?wd=amos_tl"); client.execute (request);

HTTP POST Example:

1
  public class TestHttpPostMethod (public void post () (BufferedReader in = null; try (HttpClient client = new DefaultHttpClient (); HttpPost request = new HttpPost ( "http://localhost/upload.jsp"); List <NameValuePair> postParams = new ArrayList <NameValuePair> (); postParams.add (new BasicNameValuePair ( "filename", "sex.mov")); UrlEncodeFormEntity formEntity = new UrlEncodeFormEntity (postParams); request.setEntity (formEntity); HttpResponse response = client.execute ( request); in = new BufferedReader (new InputStreamReader (response.getEntity (). getContent ())); StringBuffer sb = new StringBuffer (""); String line = ""; String NL = System.getProperty ( "line.separator "); while ((line = in.readLine ())! = null) (sb.append (line   NL);) in.close (); String result = sb.toString (); Log.i (TAG, result);) catch (Exception e) (Log.e (TAG, e.toString ())) finally (if (in! = null) (try (in.close ();) catch (IOException ioe) (Log. e (TAG, ioe.toString ());)))))

multipart POST support:

Requires the following support:
Commons IO
http://commons.apache.org/io/

Mime4j
http://james.apache.org/mime4j/

HttpMime
http://hc.apache.org/httpcomponents-client/httpmime/index.html

Download all the JAR URL:
http://www.sayedhashimi.com/downloads/android/multipart-android.zip

multipart POST Example:

1
  public class TestHttpMultipartPost (public void mulPost () (try (InputStram in = this.getAssets (). open ( "data.xml"); HttpClient client = new HttpDefaultHttpClient (); HttpPost request = new HttpPost ( "http://localhost / upload.jsp "); byte [] data = IOUtils.toByteArray (in); InputStreamBody isb = new InputStreamBody (new ByteArrayIntputStream (data)," uploadedFile "); StringBody sb1 = new StringBody (" some text "); StringBoyd sb2 = new StringBody ( "some text too"); MultipartEntity me = new MultipartEntity (); me.addPart ( "uploadedFile", isb); me.addPart ( "one", sb1); me.addPart ( "two", sb2 ); request.setEntity (me); HttpRespones response = client.excute (request); res.getEntity (). getContent (). close ();) catch (Throwable e) (Log.e (TAG, e.toString ( ));)))

Exception handling retry processing

The use of multi-threading problem ClientConnectionManager, to create a thread-safe HttpClient.

1
  public class ApplicationEx extends Application (public static final String TAG = "amos_tl"; private HttpClient client = null; @ override public void onCreate () (super.onCreate (); client = createHttpClient ();) @ override public void onLowMemory () (super.onLowMemory (); shutdownHttpClient ();) @ override public void onTerminate () (super.onTerminate (); shutdownHttpClient ();) private void shutdownHttpClient () (if (client! = null & & client.getConnectionManager ()! = null) (client.getConnectionManager (). shutdown (); client = null;)) private HttpClient createHttpClient () (Log.d (TAG, "create httpclient ..."); HttpParams params = new BasicHttpParams (); HttpProtocolParams . setVersion (params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset (params, HTTP.DEFAULT_CONTENT_CHARSET); HttpProtocolParams.setUseExpectContinue (params, true); SchemaRegistry sr = new SchemaRegistry (); sr.register (new Schema ( "http", PlainSocketFactory . getSocketFactory (), 80)); sr.register (new Schema ( "https", SLLSocketFactory.getSocketFactory (), 443)); ClientConnectionManager cm = new ThreadSafeClientConnManager (params, sr); return new DefaultHttpClient (cm, params); ) public HttpClient getHttpClient () (return client;))

HttpActivity.java

1
  public class HttpActivity extends Activity (@ override public void onCreate (Bundle savedInstanceState) (super.onCreate (savedInstanceState); Log.d (TAG, "httpactivity startup ..."); getHttpContent ();) private void getHttpContent () (try (ApplicationEx app = (ApplicationEx) this.getApplication (); HttpClient client = app.getHttpClient (); HttpGet request = new HttpGet (); request.setURI ( "http://w26.javaeye.com"); HttpResponse response = client.excute (resquest); String page = EntityUtils.toString (response.getEntity ()); Log.i (TAG, page);) catch (Exception e) (Log.e (TAG, e.toString ()); )))

Configuration AndroidManifest.xml
<application android: icon = "@ drawable / icon"
android: label = "@ string / app_name"
android: name = "ApplictionEx"
"