Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Android HTTP request with Get Information     - How to improve the performance of Ruby On Rails (Linux)

- Linux VMware virtual machine after the cloning of the card can not start to solve (Linux)

- Replace font under Linux (Linux)

- Install Ubuntu Software Center App Grid (Linux)

- CentOS 7 install Hadoop-cdh-2.6 (Server)

- Redmine Installation (Linux)

- Oracle bug Ora-04043 (Database)

- ELKstack log analysis platform (Server)

- Use GNU / Linux broadcasting of television programs (Linux)

- ORA-12537: TNS: connection closed error process (Database)

- To compiler and install MariaDB-10.0.20 under CentOS 6.6 (Database)

- JavaScript function closures Quick Start (Programming)

- Linux Firewall IPCop Profile (Linux)

- libreadline.so.6: can not open shared object file problem solution (Linux)

- Using Linux / Unix Text processing (Linux)

- Linux operating system security management skills (Linux)

- Deb package installation method under ubuntu (Linux)

- Additional SQL Server 5123 database reported error (Database)

- SSH Filesystem use a secure connection for network file system (Linux)

- Linux under HAProxy + Keepalived dual-availability program (Server)

 
         
  Android HTTP request with Get Information
     
  Add Date : 2017-08-31      
         
         
         
  On its own Android learning to be a summary, for easy viewing later.

First, the Android transmitting Get way http requests using a Java standard class, but also relatively simple.

Mainly divided into the following steps:

1. Construction URL

URL url = new URL (String path);

2. Set up a connection

httpURLConnection = (HttpURLConnection) url.openConnection ();
//overtime time
httpURLConnection.setConnectTimeout (3000);
// Indicate this http request settings using GET method
httpURLConnection.setRequestMethod ( "GET");
int responsecode = httpURLConnection.getResponseCode (); // return to the response to the number, such as: HTTP_OK indicates a successful connection.
3. Get Back Data

if (responsecode == HttpURLConnection.HTTP_OK) {
inputStream = httpURLConnection.getInputStream ();
} // Get inputStream easier to handle it.
new InputStreamReader (inputStream, "utf-8")

4. Close the connection

void disconnect ()

Second, by following a simple way to get the realization Demo Request:

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";
        private 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 the server data to return InputStream
    * @return
    * /
    public static InputStream getInputStream () {
        InputStream inputStream = null;
          
        try {
            URL url = new URL (URL_PATH);
            if (url! = null) {
                try {
                    httpURLConnection = (HttpURLConnection) url.openConnection ();
                    //overtime time
                    httpURLConnection.setConnectTimeout (3000);
                    // Indicate this http request settings using 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 locally
        // SaveImageToDisk ();
        shuchu ();
    }
}


// Because get this way is ava standard class, direct write java program, but all the same, android is also the same. .

Simple visited Baidu, Baidu search page is returned source: Image not been uploaded. . . No screenshot friends.

The right to return is that you right-click on the page to have a look at the source code, it is returned, it is output, they can go to the next comparison is not the same, is the same.
     
         
         
         
  More:      
 
- Proxmox VE implement KVM OpenVZ virtualization cloud computing (Server)
- Android LayoutInflater source parsing (Programming)
- Live Wallpaper for Linux release adds dynamic background (Linux)
- State and Linux nf_conntrack TCP disconnect time (Programming)
- Java string concatenation techniques (StringBuilder tips) (Programming)
- Ubuntu / Fedora / CentOS system how to install Plex Media Server 0.9.9 (Linux)
- C # C ++ Java interface type conversion (Programming)
- Ubuntu 14.04 LTS to compile the source code Android4.4.2 (Linux)
- Git commands (Linux)
- CentOS source installation GitLab Chinese Version (Server)
- Android design patterns - state mode (Programming)
- Linux 6 use UDEV binding shared storage (Linux)
- CentOS 6.x systems installation + NIC driver installation (Realtek PCIe GBE Family Controller for Linux) (Linux)
- ApacheDS configuration of users and user groups to achieve SSO (Server)
- CentOS 6.6 install JDK7 (Linux)
- JavaScript in null undefined summary (Linux)
- Linux folder and extract the differential file comparison techniques -rsync Magical (Linux)
- Difference Docker mirror and containers (Server)
- Shell command line operation (Linux)
- Diagnose and resolve the SSH connection slow (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.