theme: apple-basic
layout: intro
highlighter: shiki
lineNumbers: true
동작방식
| Method | 설명 |
|---|---|
| GET | 서버에 리소스를 요청합니다. 서버를 수정하지 않기 때문에 safe method로 분류. |
| HEAD | GET과 같지만 서버가 본문(body)을 포함하지 않음. |
| POST | 클라이언트에서 요청한 URL에 본문의 내용으로 새로운 리소스를 생성. |
| PUT | 클라이언트가 요청한 내용으로 서버의 리소스를 수정. |
| DELETE | 서버의 리소스를 삭제. |
| CONNECT | 요청 리소스에 대해 양방향 연결을 수립, 예로 프록시를 통한 SSL 연결수립에 사용될 수 있음. |
| OPTIONS | 서버가 어떤 HTTP 메소드를 지원하는지 물어봄. |
| TRACE | 메시지의 변조여부 확인을 위해, 서버가 수신한 클라이언트의 메시지를 반환하도록 함. |
| PATCH | PUT은 리소스 전체를 수정하나, PATCH는 해당 리소스의 일부만을 수정. |
| HTTP Method | CRUD | Entire Collection (e.g. /customers) | Specific Item (e.g. /customers/{id}) |
|---|---|---|---|
| POST | Create | 201 (Created), 'Location' header with link to /customers/{id} containing new ID. | 404 (Not Found), 409 (Conflict) if resource already exists.. |
| GET | Read | 200 (OK), list of customers. Use pagination, sorting and filtering to navigate big lists. | 200 (OK), single customer. 404 (Not Found), if ID not found or invalid. |
| PUT | Update/Replace | 405 (Method Not Allowed), unless you want to update/replace every resource in the entire collection. | 200 (OK) or 204 (No Content). 404 (Not Found), if ID not found or invalid. |
| PATCH | Update/Modify | 405 (Method Not Allowed), unless you want to modify the collection itself. | 200 (OK) or 204 (No Content). 404 (Not Found), if ID not found or invalid. |
| DELETE | Delete | 405 (Method Not Allowed), unless you want to delete the whole collection—not often desirable. | 200 (OK). 404 (Not Found), if ID not found or invalid. |
Apache에서 제작한 라이브러리
안드로이드 초기에 주로 사용되었으며 실제로는 HttpClient를 래핑한 DefaultHttpClient나, 안드로이드에 맞게 개수한 AndroidHttpClient가 사용됨.
변경점을 안드로이드 SDK에 일괄적으로 즉시 반영할 수 없었음
Android 5.1에서 Deprecated 되며 6.0에서는 아예 삭제되었음
이 시기 클라이언트의 버그는 네이버 D2 블로그 Android의 HTTP 클라이언트 라이브러리에 잘 정리되어 있음
URL url = new URL("http://www.android.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
} finally {
urlConnection.disconnect();
}
HttpUrlConnection 사용에 많은 불편함이 있어 만든 라이브러리
특징
val textView = findViewById<TextView>(R.id.text)
val queue = Volley.newRequestQueue(this)
val url = "https://www.google.com"
val stringRequest = StringRequest(Request.Method.GET, url,
Response.Listener<String> { response ->
// Display the first 500 characters of the response string.
textView.text = "Response is: ${response.substring(0, 500)}"
},
Response.ErrorListener { textView.text = "That didn't work!" })
queue.add(stringRequest)
OkHttpClient client = new OkHttpClient();
String run(String url) throws IOException {
Request request = new Request.Builder()
.url(url)
.build();
try (Response response = client.newCall(request).execute()) {
return response.body().string();
}
}
public interface GitHubService {
@GET("users/{user}/repos")
Call<List<Repo>> listRepos(@Path("user") String user);
}
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com/")
.build();
GitHubService service = retrofit.create(GitHubService.class);
Call<List<Repo>> repos = service.listRepos("octocat");
CoroutineScope(Dispatchers.IO).launch {
val client = HttpClient()
val response: HttpResponse = client.get("https://ktor.io/")
val stringBody: String = response.receive()
client.close()
}
| Function | Volley | Retrofit |
|---|---|---|
| Automatic Parsing | No | Yes |
| Caching | Yes | No |
| Retrying | Yes | No |
| Post Requests & Multipart uploads | Yes | Partly |
| Image Loading | Built-in | No |