Android sends http requests in Get mode, androidget
The annoying days have finally passed. Now I can write a blog and make a summary of my android learning so that I can view it later ......
1. In android, http requests are sent using Get, which uses java standard classes and is relatively simple.
The main steps are as follows:
1. Construct a URL
URL url = new URL (String path );
2. Set connection
HttpURLConnection = (HttpURLConnection) url. openConnection ();
// Timeout
HttpURLConnection. setConnectTimeout (3000 );
// Sets the GET method for this http request.
HttpURLConnection. setRequestMethod ("GET ");
Int responsecode = httpURLConnection. getResponseCode (); // return the critical response number. For example, HTTP_ OK indicates that the connection is successful.
3. Get the returned data
If (responsecode = HttpURLConnection. HTTP_ OK ){
InputStream = httpURLConnection. getInputStream ();
} // It is easy to get inputStream.
New InputStreamReader (inputStream, "UTF-8 ")
4. Close the connection
Void disconnect ()
2. The following is a simple Demo to implement get requests:
Package com. http. get; import java. io. bufferedReader; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. inputStreamReader; import java. io. unsupportedEncodingException; import java.net. httpURLConnection; import java.net. malformedURLException; import java.net. URL; public class HttpUtils {private static String URL_PATH = "http://www.baidu.com"; priva Te static HttpURLConnection httpURLConnection = null; public HttpUtils () {} public static void shuchu () {InputStream inputStream = getInputStream (); String result; try {BufferedReader reader = new BufferedReader (new InputStreamReader (inputStream, "UTF-8"); result = ""; String line = ""; try {while (line = reader. readLine ())! = Null) {result = result + line;} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();} System. out. println (result); httpURLConnection. disconnect ();} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch block e. printStackTrace () ;}}/*** get server data, return * @ return */public static InputStream getInputStream () {InputStream = null; t Ry {URL url = new URL (URL_PATH); if (url! = Null) {try {httpURLConnection = (HttpURLConnection) url. openConnection (); // timeout value: httpURLConnection. setConnectTimeout (3000); // sets the http request to use the GET method httpURLConnection. setRequestMethod ("GET"); int responsecode = httpURLConnection. getResponseCode (); if (responsecode = HttpURLConnection. HTTP_ OK) {inputStream = httpURLConnection. getInputStream () ;}} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} catch (MalformedURLException e) {// TODO Auto-generated catch block e. printStackTrace () ;}return inputStream;} public static void main (String [] args) {// save the file to the local machine // saveImageToDisk (); shuchu ();}}
// Because the get method is the java program directly written in this ava standard class, it is the same in both android and android ..
After a simple access to Baidu, the source code of the baidu search homepage is returned: Images cannot be uploaded all the time... No.
The correct response is that you right-click the webpage and have a view of the source code. The returned result is the same as the output. You can compare it with the output ..