# HTTP GET

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

```java
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 要另外解析

{% embed url="https://dotblogs.com.tw/newmonkey48/2017/09/21/152425" %}

### 包成 class

```java
package com.example.myapplication;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.URL;

public class Util {
    public interface GenericMethod { // 讓呼叫可以使用 callback
        public void call(String s);
    }
    public static String doGet(String _url, GenericMethod g) throws InterruptedException {
        StringBuilder response = new StringBuilder();

        Thread threadC = new Thread(() -> {
                try {
                    URL url = new URL(_url);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    InputStream is = connection.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                    String line;


                    while ((line = reader.readLine()) != null) {
                        response.append(line);
                        response.append('\r');
                    }
                    reader.close();
                    g.call(response.toString());
                } catch (IOException e) {
                    e.printStackTrace();
                }
        });
        threadC.start();
        return response.toString();
    }
}
```

main.java&#x20;

```java
package com.example.myapplication;

import android.os.Bundle;
import android.widget.Button;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button b = findViewById(R.id.button1);
        b.setOnClickListener(view -> {
            try {
                Util.doGet("https://tw.yahoo.com", (String s) -> {
                    System.out.println(s);
                });
            } catch (InterruptedException e){
                System.out.println(e);
            }
        });
    }
}
```

[https://stackoverflow.com/a/9148954/4622645](https://stackoverflow.com/a/9148954/4622645)

## 使用 volley

{% embed url="https://developer.android.com/training/volley/request" %}



> If the response is in JSON format, use any third-party JSON parsers such as [_Jackson_ ](https://www.baeldung.com/jackson)library, [_Gson_](https://www.baeldung.com/gson-string-to-jsonobject), or [_org.json_](https://www.baeldung.com/java-org-json) to parse the response.
