okhttp api사용에서 callback함수 오류

20.03.22 23:25 작성 조회수 730

0

build.gradle에서 implementation한 okhttp api는 

implementation "com.squareup.okhttp3:okhttp:4.4.0"

위 버전으로 진행하였는데 현재 버전에서 버전 차이로 인한 문제인지 callback함수에서 response 호출에 에러가 발생하네요

어찌 해서 다른 방식으로 호출하는 방식을 찾긴 하였는데 다른 방식은 response에 받은 http string에서 body 값을 받아 오진 못하는 문제가 발생하여서 일단은 전체 과정을 스탑한 상태이네요

inner calss로 callback을 만들때 okhttp api의 callback함수를 생성하여 과정과 동일하게 호출하였는데 앱 중단이 뜨고 내부 쓰레드에서의 앱중단이니 아마 Main UI에서 충돌 문제로 인해서 발생한다고 생각하는데 원인을 찾을 수 없네요.

callback, response에서도 response에 body를 호출하는 방식도 강의와 동일한 방식으로 진행할 경우 신텍스 에러가 뜨고 있어서 사용하지 못하고 있네요

다른 방식으로 body에 string을 result로 받아서 진행을 하여도 동일한 에러가 발생합니다.

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

button.setOnClickListener { view->
var thread = NetworkThread()
thread.start()
}
}

inner class NetworkThread : Thread(){
override fun run() {

var client = OkHttpClient()

var builder = Request.Builder()
var url = builder.url("http://google.com")
var request = url.build()
var cb = Callback2()
client.newCall(request).enqueue(cb)
/*
client.newCall(request).execute().use{ response->
if(!response.isSuccessful) throw IOException("Unexpected code $response")
else {
for ((name, value) in response.headers) {
Log.d("header", value)
}

var result =response.body!!.toString()
Log.d("result", result)
val responseBodyString = response.body!!.string()
runOnUiThread {
textView.text = response.body!!.string()
}

}
}
//client.newCall(request).enqueue(callback)
*/
}
}

inner class Callback2 : Callback{
override fun onResponse(call: Call, response: Response) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
var result = response.body!!.string()
runOnUiThread {
//textView.text = result
textView.setText(result)
}
}

override fun onFailure(call: Call, e: IOException) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}

inner class Callback1 :Callback
{
override fun onResponse(call: Call, response: Response) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.

}

override fun onFailure(call: Call, e: IOException) {

}
}

답변 2

·

답변을 작성해보세요.

0

redtree님의 프로필

redtree

질문자

2020.03.23

다음과 같은 충돌 에러가 뜨네요 

Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.

I/zygote64: Rejecting re-init on previously-failed class java.lang.Class<okhttp3.internal.platform.ConscryptPlatform$configureTrustManager$1>: java.lang.NoClassDefFoundError: Failed resolution of: Lorg/conscrypt/ConscryptHostnameVerifier;

        at okhttp3.internal.platform.android.SocketAdapter okhttp3.internal.platform.android.ConscryptSocketAdapter$Companion.buildIfSupported() (ConscryptSocketAdapter.kt:62)

        at void okhttp3.internal.platform.AndroidPlatform.<init>() (AndroidPlatform.kt:44)

        at okhttp3.internal.platform.Platform okhttp3.internal.platform.AndroidPlatform$Companion.buildIfSupported() (AndroidPlatform.kt:212)

        at okhttp3.internal.platform.Platform okhttp3.internal.platform.Platform$Companion.findPlatform() (Platform.kt:212)

        at okhttp3.internal.platform.Platform okhttp3.internal.platform.Platform$Companion.access$findPlatform(okhttp3.internal.platform.Platform$Companion) (Platform.kt:169)

        at void okhttp3.internal.platform.Platform.<clinit>() (Platform.kt:170)

        at void okhttp3.OkHttpClient.<init>(okhttp3.OkHttpClient$Builder) (OkHttpClient.kt:224)

        at void okhttp3.OkHttpClient.<init>() (OkHttpClient.kt:216)

I/zygote64:     at void com.example.network_project2.MainActivity$NetworkThread.run() (MainActivity.kt:22)

    Caused by: java.lang.ClassNotFoundException: Didn't find class "org.conscrypt.ConscryptHostnameVerifier" on path: DexPathList[[zip file "/data/app/com.example.network_project2-Mp_L8AN3j6KqUzJuKsCTUw==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.network_project2-Mp_L8AN3j6KqUzJuKsCTUw==/lib/x86_64, /system/lib64, /vendor/lib64]]

        at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:125)

        at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:379)

        at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:312)

        at okhttp3.internal.platform.android.SocketAdapter okhttp3.internal.platform.android.ConscryptSocketAdapter$Companion.buildIfSupported() (ConscryptSocketAdapter.kt:62)

        at void okhttp3.internal.platform.AndroidPlatform.<init>() (AndroidPlatform.kt:44)

        at okhttp3.internal.platform.Platform okhttp3.internal.platform.AndroidPlatform$Companion.buildIfSupported() (AndroidPlatform.kt:212)

        at okhttp3.internal.platform.Platform okhttp3.internal.platform.Platform$Companion.findPlatform() (Platform.kt:212)

        at okhttp3.internal.platform.Platform okhttp3.internal.platform.Platform$Companion.access$findPlatform(okhttp3.internal.platform.Platform$Companion) (Platform.kt:169)

        at void okhttp3.internal.platform.Platform.<clinit>() (Platform.kt:170)

        at void okhttp3.OkHttpClient.<init>(okhttp3.OkHttpClient$Builder) (OkHttpClient.kt:224)

I/zygote64:     at void okhttp3.OkHttpClient.<init>() (OkHttpClient.kt:216)

        at void com.example.network_project2.MainActivity$NetworkThread.run() (MainActivity.kt:22)

    Rejecting re-init on previously-failed class java.lang.Class<okhttp3.internal.platform.ConscryptPlatform$configureTrustManager$1>: java.lang.NoClassDefFoundError: Failed resolution of: Lorg/conscrypt/ConscryptHostnameVerifier;

        at okhttp3.internal.platform.android.SocketAdapter okhttp3.internal.platform.android.ConscryptSocketAdapter$Companion.buildIfSupported() (ConscryptSocketAdapter.kt:62)

        at void okhttp3.internal.platform.AndroidPlatform.<init>() (AndroidPlatform.kt:44)

        at okhttp3.internal.platform.Platform okhttp3.internal.platform.AndroidPlatform$Companion.buildIfSupported() (AndroidPlatform.kt:212)

        at okhttp3.internal.platform.Platform okhttp3.internal.platform.Platform$Companion.findPlatform() (Platform.kt:212)

        at okhttp3.internal.platform.Platform okhttp3.internal.platform.Platform$Companion.access$findPlatform(okhttp3.internal.platform.Platform$Companion) (Platform.kt:169)

        at void okhttp3.internal.platform.Platform.<clinit>() (Platform.kt:170)

        at void okhttp3.OkHttpClient.<init>(okhttp3.OkHttpClient$Builder) (OkHttpClient.kt:224)

        at void okhttp3.OkHttpClient.<init>() (OkHttpClient.kt:216)

        at void com.example.network_project2.MainActivity$NetworkThread.run() (MainActivity.kt:22)

    Caused by: java.lang.ClassNotFoundException: Didn't find class "org.conscrypt.ConscryptHostnameVerifier" on path: DexPathList[[zip file "/data/app/com.example.network_project2-Mp_L8AN3j6KqUzJuKsCTUw==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.network_project2-Mp_L8AN3j6KqUzJuKsCTUw==/lib/x86_64, /system/lib64, /vendor/lib64]]

        at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:125)

I/zygote64:     at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:379)

        at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:312)

        at okhttp3.internal.platform.android.SocketAdapter okhttp3.internal.platform.android.ConscryptSocketAdapter$Companion.buildIfSupported() (ConscryptSocketAdapter.kt:62)

        at void okhttp3.internal.platform.AndroidPlatform.<init>() (AndroidPlatform.kt:44)

        at okhttp3.internal.platform.Platform okhttp3.internal.platform.AndroidPlatform$Companion.buildIfSupported() (AndroidPlatform.kt:212)

        at okhttp3.internal.platform.Platform okhttp3.internal.platform.Platform$Companion.findPlatform() (Platform.kt:212)

        at okhttp3.internal.platform.Platform okhttp3.internal.platform.Platform$Companion.access$findPlatform(okhttp3.internal.platform.Platform$Companion) (Platform.kt:169)

        at void okhttp3.internal.platform.Platform.<clinit>() (Platform.kt:170)

        at void okhttp3.OkHttpClient.<init>(okhttp3.OkHttpClient$Builder) (OkHttpClient.kt:224)

        at void okhttp3.OkHttpClient.<init>() (OkHttpClient.kt:216)

        at void com.example.network_project2.MainActivity$NetworkThread.run() (MainActivity.kt:22)

D/NetworkSecurityConfig: No Network Security Config specified, using platform default

E/AndroidRuntime: FATAL EXCEPTION: Thread-2

    Process: com.example.network_project2, PID: 16577

    java.net.SocketException: socket failed: EACCES (Permission denied)

        at java.net.Socket.createImpl(Socket.java:487)

        at java.net.Socket.getImpl(Socket.java:547)

        at java.net.Socket.setSoTimeout(Socket.java:1175)

        at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.kt:281)

        at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:195)

        at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:233)

        at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:107)

        at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:75)

        at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:245)

        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32)

        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)

        at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:82)

        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)

        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83)

        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)

        at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:74)

        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)

        at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:197)

        at okhttp3.internal.connection.RealCall.execute(RealCall.kt:148)

        at com.example.network_project2.MainActivity$NetworkThread.run(MainActivity.kt:27)

D/EGL_emulation: eglMakeCurrent: 0x762114e3dfa0: ver 3 1 (tinfo 0x762114e0d4a0)

D/OpenGLRenderer: endAllActiveAnimators on 0x7621123ff800 (RippleDrawable) with handle 0x76210776f600

0

response.body!!.string()

일단 위의 부분에서 response.body 를 출력해보시기 바랍니다. 아마도 body에 null이 들어가는 걸로 보입니다.

전체 오류메시지도 같이 올려주시면 답변에 도움이 될것같습니다. 

응답 결과가 제대로 도착하지 않는 것으로 보입니다. 오류 메시지까지 확인을 해야 알 수 있을 것 같습니다.

채널톡 아이콘