okhttp3 application 2fjson get kotlin

Solutions on MaxInterview for okhttp3 application 2fjson get kotlin by the best coders in the world

showing results for - "okhttp3 application 2fjson get kotlin"
Caitlan
09 Sep 2017
1val BASE_URL = "https://api.unsplash.com"
2val ACCESS_KEY = "..."
3
4val path = "/photos/$id"
5val uri = Uri.parse(BASE_URL)
6    .buildUpon()
7    .appendEncodedPath(path)
8    //.appendPath(path)
9    .build()
10
11val client = OkHttpClient()
12val request = Request.Builder()
13    .url(uri.toString())
14    .addHeader("Accept-Version", "v1")
15    .addHeader("Authorization", "Client-ID $ACCESS_KEY")
16    .get()
17    .build()
18
19val response = client.newCall(request).execute()
20val jsonDataString = response.body()?.string()
21
22val json = JSONObject(jsonDataString)
23if (!response.isSuccessful) {
24    val errors = json.getJSONArray("errors").join(", ")
25    throw Exception(errors)
26}
27val rawUrl = json.getJSONObject("urls").getString("raw")
28