HTTP GET

必須要用 thread 發送請求,且要包住 try catch

new Thread(new Runnable(){
     @Override
     public void run() {
         try {
             URL url = new URL("https://google.com");
             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
             connection.setRequestMethod("GET");
             InputStream is = connection.getInputStream();
             BufferedReader reader = new BufferedReader(new InputStreamReader(is));
             String line;
             StringBuilder response = new StringBuilder();

             while ((line = reader.readLine()) != null) {
                 response.append(line);
                 response.append('\r');
             }
             reader.close();
             System.out.println(response);
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
 }).start();

需要讀 stream 然後 append 到字串,如果是 JSON 要另外解析

包成 class

main.java

https://stackoverflow.com/a/9148954/4622645

使用 volley

If the response is in JSON format, use any third-party JSON parsers such as Jackson library, Gson, or org.json to parse the response.

Last updated

Was this helpful?