'HttpUrl'에 해당되는 글 1건

  1. 2021.10.27 okhttp 사용(파라미터 전달)

https://github.com/square/okhttp

 

GitHub - square/okhttp: Square’s meticulous HTTP client for the JVM, Android, and GraalVM.

Square’s meticulous HTTP client for the JVM, Android, and GraalVM. - GitHub - square/okhttp: Square’s meticulous HTTP client for the JVM, Android, and GraalVM.

github.com

 

http호출을 간편하게 할 수 있는 소스

 

연결 중 get방식 파라미터를 추가 해야 하는 경우 HttpUrl을 이용해서 전달하려는 파라미터를 추가 할 수 있습니다.

https://www.googleapis.com/youtube/v3/search?part=snippet&q=kpop+music&key=apikey 이런 정보를 요청하는 경우 아래의 코드 처럼 작업 해주면 됩니다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<code class="hljs csharp">HttpUrl httpUrl = <span class="hljs-keyword">new</span> HttpUrl.Builder()
                             .scheme(API_SCHEME)
                             .host(API_HOST)
                             .addPathSegment(<span class="hljs-string">"youtube"</span>)
                             .addPathSegment(<span class="hljs-string">"v3"</span>)
                             .addPathSegment(<span class="hljs-string">"search"</span>)
                             .addQueryParameter(<span class="hljs-string">"part"</span>, <span class="hljs-string">"snippet"</span>)
                             .addQueryParameter(<span class="hljs-string">"q"</span>, <span class="hljs-string">"kpop+music"</span>)
                             .addQueryParameter(<span class="hljs-string">"key"</span>, apikey)
                             .build();
System.<span class="hljs-keyword">out</span>.println(httpUrl.toString());
             
Request request = <span class="hljs-keyword">new</span> Request.Builder()
                              .url(httpUrl)
                              .<span class="hljs-keyword">get</span>()
                              .build();
 
</code>
반응형
Posted by 질주하는구
,