티스토리 뷰
build.gradle (Module: app)
dependencies {
// Retrofit2
implementation 'com.squareup.retrofit2:retrofit:2.7.1'
implementation 'com.squareup.retrofit2:converter-gson:2.7.1'
implementation 'com.squareup.okhttp3:logging-interceptor:4.3.1'
}
AndroidManifast.xml
<uses-permission android:name="android.permission.INTERNET"/>
인터넷 권한 추가.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jwsoft.kotlinproject">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
RetrofitAPI.kt
package com.jwsoft.kotlinproject
import com.google.gson.JsonObject
import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Path
interface RetrofitAPI {
@GET("/users/{user}/repos")
fun getUserRepos(@Path("user") userName: String): Call<List<JsonObject>>
}
MainActivity.kt
package com.jwsoft.kotlinproject
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.util.Log
import com.google.gson.JsonObject
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var interceptor = HttpLoggingInterceptor(object: HttpLoggingInterceptor.Logger {
override fun log(message: String) {
Log.e("", message)
}
})
interceptor.level = HttpLoggingInterceptor.Level.BODY
var client = OkHttpClient.Builder()
.connectTimeout(5, TimeUnit.SECONDS)
.readTimeout(5, TimeUnit.SECONDS)
.writeTimeout(5, TimeUnit.SECONDS)
.addInterceptor(interceptor)
.build()
var retrofit: Retrofit = Retrofit.Builder()
.baseUrl("https://api.github.com")
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build()
var retrofitAPI = retrofit.create(RetrofitAPI::class.java)
retrofitAPI.getUserRepos("jwsoft91").enqueue(object: Callback<List<JsonObject>> {
override fun onFailure(call: Call<List<JsonObject>>, t: Throwable) {
Log.e("", "fail !!!")
}
override fun onResponse(
call: Call<List<JsonObject>>,
response: Response<List<JsonObject>>
) {
var jsonArray = response.body()
var jsonObject = jsonArray?.get(0)
Log.e("", jsonObject?.get("name").toString())
}
})
}
}
build.gradle (Module: app)
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
위에 코드 추가하면 에러 해결됨.
stackoverflow.com/questions/59448845/no-static-method-metafactory
실행 후
위 에러 발생 시 앱 삭제 후 다시 실행하면 됨.
최종 실행 결과
'Android > Kotlin' 카테고리의 다른 글
[Kotlin] AsyncTask (0) | 2020.06.29 |
---|---|
[Kotlin] runOnUiThread (0) | 2020.06.28 |
[Kotlin] Handler.post (0) | 2020.06.27 |
[Kotlin] Handler (0) | 2020.06.27 |
[Kotlin] RecyclerView (0) | 2020.06.26 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- DataBinding
- ViewPager2
- Design Pattern
- ViewModel
- 혀가 길지 않은 개발자
- fragment
- recyclerview
- java
- Android
- Livedata
- 안드로이드 #코틀린 #Android #Kotlin
- JSONArray
- Kotlin
- coroutine
- ArrayList
- JSONObject
- 코틀린
- activity
- Architecture Pattern
- 안드로이드
- CoordinatorLayout
- XML
- handler
- Intent
- TabLayout
- James Kim
- View
- MVVM
- Vue.js #Vue.js + javascript
- 자바
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함