Free Source Code and Program Tips
Android HTTP Service
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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 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"
"
| Print article | This entry was posted by support on November 21, 2009 at 10:44 am, and is filed under General. Follow any responses to this post through RSS 2.0. You can skip to the end and leave a response. Pinging is currently not allowed. |
about 5 months ago
Im very dissapponted with ur site.
It is not at all userfrendly.
To understand the code we need to struggle like hell.
Insted you can place the code in more userfriendly readable format.
Many times I skipped ur site though i got my answer from ur site ,Just it is very tough to read the code in a single line.
It will be very help full if u make it as readers friendly.
One thing ur post are very good but not reader friendly.
Regards
Kumar