SoFunction
Updated on 2025-04-06

Android custom network connection tool class HttpUtil

This article shares the usage method of the Android network connection tool HttpUtil for your reference. The specific content is as follows

This tool implements sending get and post requests, and the result of the request is returned in the form of a String string, which is more suitable for receiving JSON string data sent by the server side.

The get method is suitable for obtaining data from the server side
The post method is suitable for sending data to the server

Just call the get or post method when using it

The get method passes a url request
The post method passes a url request and the data to be sent to the server side params

After receiving the data, it returns a String string


public class HttpUtil{ 
  /**
    * The POST method submits an HTTP request and returns the request result
    *
    * @param url
    * @param params
    * @return Request result
    * @throws IOException
    */ 
  public static String sendPost(String url, String params) throws IOException { 
    StringBuffer result = new StringBuffer(); 
 
    // Create URL object    URL _url = new URL(url); 
    // Create HTTP connection    /**
      * Instantiate a URLConnection object using the .openConnection() method
      * */ 
    HttpURLConnection conn = (HttpURLConnection) _url.openConnection(); 
     
    // The following settings are related parameters for network connection    /* When using the POST method for request delivery, the setDoInput and setDoOutput methods must be defined */ 
    // Setting input is available    (true); 
    // Set the output available    (true); 
 
    // Set not to use cache    (false); 
    // Set the connection timeout time - 5s    (5000); 
    // Set the timeout for read timeout - 5s    (5000); 
    // Setting the HTTP request method - POST    ("POST"); 
    // Set HTTP request attributes - Connection method: Keep    ("Connection", "Keep-Alive"); 
    // Set HTTP request attributes - Character set: UTF-8    ("Charset", "UTF-8"); 
    // Set HTTP request attributes - Type of content to be transferred - Simple form    ("Content-Type", 
        "application/x-www-form-urlencoded"); 
    // Set HTTP request attributes - length of content transmitted    ("Content-Length", 
        (())); 
    // Set HTTP request attributes - User Agent    ("User-Agent", 
        "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0"); 
    // Send parameters, send data using character stream    PrintWriter pw = new PrintWriter(()); 
    (params); 
    (); 
    (); 
    // Get the returned result    if (200 == ()) {// Status code      // Read the result returned by the server - Character stream      BufferedReader br = new BufferedReader(new InputStreamReader( 
          ())); 
      // Read one line at a time      String line; 
      while((line = ()) != null){ 
        (line); 
      } 
    } 
    // Close HTTP connection    (); 
    return (); 
  } 
 
  /**
    * GET method submits an HTTP request and returns the request result
    * @param url
    * @return Result of request
    * @throws IOException
    */ 
  public static String sendGet(String url) throws IOException { 
 
    StringBuffer result = new StringBuffer(); 
    // Create URL object    URL _url = new URL(url); 
    // Create HTTP connection    HttpURLConnection conn = (HttpURLConnection) _url.openConnection(); 
    // Set relevant parameters for network connection    // Setting input is available    (true); 
    // Set the output available    (true); 
    // Set not to use cache    (false); 
    // Set the connection timeout time - 5s    (5000); 
    // Set the timeout for read timeout - 5s    (5000); 
    // How to set HTTP request - GET    ("GET"); 
    // Set HTTP request attributes - Connection method: Keep    ("Connection", "Keep-Alive"); 
    // Set HTTP request attributes - Character set: UTF-8    ("Charset", "UTF-8"); 
    // Set HTTP request attributes - User Agent    ("User-Agent", 
        "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0"); 
    // Get the returned result    if (200 == ()) {// Status code      BufferedReader br = new BufferedReader(new InputStreamReader( 
          ())); 
      // Read one line at a time      String line; 
      while((line = ()) != null){ 
        (line); 
      } 
    } 
    // Close HTTP connection    (); 
    return (); 
  } 
} 

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.