SoFunction
Updated on 2025-03-02

How to access the network using HttpURLConnection for Android

1. HttpURLConnection accesses the network through GET:

HttpURLConnection connection = null;
try {
 URL url = new URL("/");
 connection = (HttpURLConnection) ();
 ("GET");//Set the access method to "GET" (8000);//Set the timeout time for connection to server to 8 seconds (8000);//Set the timeout time to read server data to 8 seconds
 if (HttpURLConnection.HTTP_OK == ()) {
  //Get the response from the server and convert the response data into a string to print  InputStream in = ();
  BufferedReader reader = new BufferedReader(new InputStreamReader(in));

  StringBuilder response = new StringBuilder();
  String line;
  while (null != (line = ())) {
    (line);
  }
  (TAG, ());
 }
} catch (Exception e) {
 ();
} finally {
 if (null!= connection) {
   ();
 }
}

2. HttpURLConnection accesses the network through POST:

HttpURLConnection connection = null;
  try{
   URL url = new URL("/");
   connection = (HttpURLConnection) ();

   ("POST");
   (8000);
   (8000);
   (true);// Use URL connection for output   (true);// Use URL connection to enter   (false);// Ignore cache
   // Create output stream and write data   OutputStream outputStream = ();
   DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
   ("username=admin&password=888888");
   ();

   if (HttpURLConnection.HTTP_OK == ()) {
    // Process data when responding correctly    StringBuffer response = new StringBuffer();
    String line;
    BufferedReader responseReader = 
      new BufferedReader(new InputStreamReader((), "utf-8"));
    // To process the response stream, it must be consistent with the encoding output of the server response stream    while (null != (line = ())) {
     (line);
    }
    ();
    (TAG, ());
   }

  } catch (Exception e) {
   ();
  } finally {
   if (null!= connection) {
    ();
   }
  }

Notice:

1. HTTP access is not allowed on the main thread, otherwise an error will be reported. Therefore, the above operation should be performed in a new thread.

2. Generally, use () == 200 to determine whether the response is normal. If true, the response is normal.

3. In Android 2.2 and below, HttpClient is used, Android 2.3 and above, HttpURLConnection is used, and the relevant APIs of HttpClient are abandoned after Android 5.1. Therefore, the usage of HttpClient will no longer be studied.

4. When submitting data in POST, each piece of data must be submitted in the form of key-value pairs, and each piece of data must be separated by &. For example, in the above code ("username=admin&password=888888");

5. The above uses StringBuilder and StringBuffer, but there is no special intention, just use it by the way. StringBuilder is more efficient than StringBuffer on a single thread, but is not thread-safe.

The above method of accessing the network with HttpURLConnection on Android is all the content I share with you. I hope you can give you a reference and I hope you can support me more.