Original works, allow reprint, reprint, please be sure to hyperlink form to indicate the original source of the article, author information and this statement. Otherwise, legal liability will be held. http://liangruijun.blog.51cto.com/3061169/803097
In Android development, the Android SDK comes with Apache HttpClient, which is a perfect client. It provides full support for the HTTP protocol, and you can use HttpClient objects to perform HTTP GET and HTTP POST calls.
How HTTP Works:
1. Client (generally refers to the browser, here refers to their own written program) and the server to establish a connection
2. After establishing the connection, the client sends the request to the server
3. After the server receives the request, sends the response information to the client
4. Client disconnects from server
HttpClient General steps to use:
1. Instantiating HttpClient objects using the Defaulthttpclient class
2. Create a HttpGet or HttpPost object that will pass the URL of the request to the HttpGet or HttpPost object by constructing the method.
3. Invoke the Execute method to send an HTTP GET or HTTP POST request and return the HttpResponse object.
4. Return the response information through the GetEntity method of the HttpResponse interface and handle it accordingly.
Finally remember to add network permissions to the Androidmanifest.xml file
<uses-permission android:name= "Android.permission.INTERNET"/>
Here are the specific examples:
1. Use HttpClient to perform a get call
You can see the output information in the Logcat window
package com.lingdududu.http; import java.io.inputstream; import org.apache.http.httpresponse; import org.apache.http.httpstatus; import org.apache.http.client.httpclient; import org.apache.http.client.methods.httpget; import org.apache.http.impl.client.defaulthttpclient; import android.app.activity; import android.os.bundle; import android.util.log; public class httpgetactivity extends activity { String uri = "http://developer.android.com/"; final String TAG_STRING = "TAG"; @Override public void oncreate (Bundle savedinstancestate) { super.oncreate ( Savedinstancestate); &NBSP;&NBSP; setcontentview (R.layout.main); try { //get HttpClient object HttpClient getClient = New defaulthttpclient (); //get HttpGet object httpget request = new httpget ( URI); //clients use Get method to perform consulting, Get server-side response response httpresponse response = getclient.execute (Request); Determine if the request was successful &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;IF ( Response.getstatusline (). Getstatuscode () ==httpstatus.sc_ok) { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.I (tag_string, "Request Server-side success"); //Get input stream Inputstream instrem = response.getentity (). getcontent (); int result = instrem.read (); while (result != -1) { &Nbsp; system.out.print ((char) result); result = instrem.read (); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;} //turn off the input stream instrem.close ( ); }else { LOG.I (tag_string, "Request Server Side failure"); } } catch (exception e) { // todo auto-generated catch block e.printstacktrace (); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} } }
One disadvantage of using HTTP GET calls is that the requested parameter is passed as part of the URL, and the length of the URL should be within 2048 characters when passed in this way. If you exceed this range, you will need to use an HTTP POST call.
2. Use HttpClient to perform post calls
When you use a post call for parameter passing, you need to use Namevaluepair to hold the arguments that you want to pass. Namevaluepair encapsulates a key/value combination. In addition, you need to set the character set that you are using.
Package com.androidbook.services.httppost; Import Java.io.BufferedReader; Import java.io.IOException; Import Java.io.InputStreamReader; Import java.util.ArrayList; Import java.util.List;