Android HttpURLConnection GET client gets json data from server

First of all, thanks for the great essence articles on many editions, so that I can learn a lot of information as a rookie and complete this program

The following is a brief introduction to how HttpURLConnection  accesses the server from the App

1. If you are establishing a server , the IP address used below only needs to change the location where the files are stored later. If it is not a local server, you can directly change it to the physical IP address of the server.

2. The HttpURLConnection object needs to be executed in a thread . The following program code can be directly copied to your Class and used. In the new Thread subroutine, the thread has been used, and there is no need to write another program to process it.

3. At the end of the thread, you must remember .start(); so as not to debug for a long time and have not found the problem, it turns out that you forgot to start, which is stupid

private void getJSON()
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                //default IP address
                String host = "http://10.0.2.2/SelectData.php";
                //Create HttpURLConnection object
                HttpURLConnection conn = null;
                try
                {
                    //Check if the thread is normal
                    if(Thread.interrupted()) throw new InterruptedException();
                    //set the link
                    URL url = new URL(host);
                    // open the link
                    conn = (HttpURLConnection) url.openConnection();
                    //Set the read link time, here is set to 10 seconds
                    conn.setReadTimeout(10000);
                    //Set the online link time, here is set to 15 seconds
                    conn.setConnectTimeout(15000);
                    //Use GET method to get data
                    conn.setRequestMethod("GET");
                    //URL connections can be used for input and/or output. Set the DoInput flag to true if you intend to use the URL connection for input; if you do not intend to use it, set it to false. The default value is true. 
                    conn.setDoInput(true);
                    //connect to the server
                    conn.connect();
                    //Check if the thread is normal
                    if(Thread.interrupted()) throw new InterruptedException();
                    // load data with buffer
                    BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
                    // read JSON line by line
                    String jsonString = reader.readLine();
                    // print JSON information
                    Log.d("jsonStr",jsonString);
                    //close the object
                    reader.close();
                }
                catch (Exception e)
                {
                    // there is an error, throw an exception
                    e.printStackTrace ();
                }
                finally
                {
                    //If the connection is successful, finally close the connection
                    if(conn != null) conn.disconnect();
                }
            }
        //finally start the thread
        }).start();
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324838911&siteId=291194637