Android sends http requests in Get mode, androidget

Source: Internet
Author: User

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 ..

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.