The HttpURLConnection and HttpClient interfaces provided in Android can be used to develop HTTP programs. Here are some experiences from learning.
1. HttpURLConnection interface
First of all, it should be clear that the POST and GET request methods in Http communication are different. GET can obtain static pages, or you can place parameters behind URL strings and pass them to the server. The parameters of the POST method are placed in the Http request. Therefore, before programming, the request method used should be first clarified, and then the corresponding programming method should be selected according to the method used. HttpURLConnection is inherited from the URLConnection class, both of which are abstract classes. Its object is mainly obtained through the openConnection method of the URL. The creation method is shown in the following code:
URL url = new URL("/?type=231");
HttpURLConnection urlConn=(HttpURLConnection)();
The requested properties can be set by the following method, as shown below:
//Set input and output streams
(true);
(true);
//Set the request method to POST
("POST");
// POST request cannot be used
(false);
();
HttpURLConnection uses GET by default, for example, as shown in the following code:
HttpURLConnection urlConn = (HttpURLConnection) ();
InputStreamReader in = new InputStreamReader(());
BufferedReader buffer = new BufferedReader(in);
String inputLine = null;
while (((inputLine = ()) != null)) {
resultData += inputLine + "\n";
}
();
();
If you need to use the POST method, you need to setRequestMethod. The code is as follows:
String httpUrl = "/";
String resultData = "";
URL url = null;
try {
url = new URL(httpUrl);
} catch (MalformedURLException e) {
(DEBUG_TAG, "MalformedURLException");
}
if (url != null) {
try {
HttpURLConnection urlConn = (HttpURLConnection) ();
//Because this is a post request, the setup needs to be set to true
(true);
(true);
("POST"); // Set in POST mode
(false);
(true);
("Content-Type","application/x-www-form-urlencoded");
// These configurations must be completed before connect,
// It should be noted that it will implicitly connect.
();
// DataOutputStream Stream
DataOutputStream out = new DataOutputStream(());
String content = "name=" + ("Zhang San", "GB2312");
(content);
();
();
} catch(Exception e) {
//
}
}
2. HttpClient interface
HTTP operations can also be performed using the HttpClient interface provided by Apache. Operations for GET and POST request methods are different. The operation code example of the GET method is as follows:
String httpUrl = "/?par=123";
// HttpGet connection object
HttpGet httpRequest = new HttpGet(httpUrl);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse httpResponse = (httpRequest);
// Request succeeded
if (().getStatusCode() == HttpStatus.SC_OK) {
// Get the returned string
String strResult = (());
(strResult);
} else {
("Request error!");
}
When passing parameters using POST method, you need to use NameValuePair to save the parameters to be passed. In addition, you need to set the character set used. The code looks like this:
String httpUrl = "/";
HttpPost httpRequest = new HttpPost(httpUrl);
List<NameValuePair> params = new ArrayList<NameValuePair>();
(new BasicNameValuePair("userId", "123"));
HttpEntity httpentity = new UrlEncodedFormEntity(params, "GB2312"); //Set the character set
(httpentity);
//Get the default HttpClient
HttpClient httpclient = new DefaultHttpClient();
//Get HttpResponse
HttpResponse httpResponse = (httpRequest);
//HttpStatus.SC_OK means the connection is successful
if (().getStatusCode() == HttpStatus.SC_OK) {
// Get the returned string
String strResult = (());
(strResult);
} else {
("Request error!");
}
HttpClient is actually some encapsulation of Java methods. The input and output stream operations in HttpURLConnection are uniformly encapsulated into HttpPost (HttpGet) and HttpResponse in this interface, which reduces the cumbersomeness of the operation.
The above is the entire content of this article, I hope you like it.