OkHttp3不与Kotlin(Android)一起工作,给出了一个致命错误
我在Android Studio开发中使用OkHttp3和Kotlin时面临着相当大的困难,因为它一直告诉我我有一个“致命错误”,尽管我遵循了使用OkHttp3的步骤。我也在清单文件中添加了internet权限。下面是我的标题片段代码:OkHttp3不与Kotlin(Android)一起工作,给出了一个致命错误,android,kotlin,okhttp,Android,Kotlin,Okhttp,我在Android Studio开发中使用OkHttp3和Kotlin时面临着相当大的困难,因为它一直告诉我我有一个“致命错误”,尽管我遵循了使用OkHttp3的步骤。我也在清单文件中添加了internet权限。下面是我的标题片段代码: package com.example.materialdesigntest import android.os.Bundle import android.util.Log import androidx.fragment.app.Fragment impo
package com.example.materialdesigntest
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.navigation.fragment.findNavController
import okhttp3.*
import java.io.IOException
class TitleFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_title, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val signInButton: Button = view.findViewById(R.id.signInButton)
val createAccountButton: Button = view.findViewById(R.id.createAccountButton)
val internetButton: Button = view.findViewById(R.id.internetButton)
val endText: TextView = view.findViewById(R.id.internetResponse)
var client1 = OkHttpClient()
var request1 = Request.Builder()
.url("https://jsonplaceholder.typicode.com/posts")
.build()
client1.newCall(request1).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
TODO("Not yet implemented")
}
override fun onResponse(call: Call, response: Response) {
TODO("Not yet implemented")
endText.text = response.body.toString()
}
})
Log.i("TitleFragment", "request Complete.")
createAccountButton.setOnClickListener {
findNavController().navigate(R.id.action_titleFragment_to_createAccountFragment)
}
}
}
我还附上了来自Logcat的错误日志:
2020-05-20 10:18:32.821 31008-31008/com.example.materialdesigntest D/InputMethodManager: startInputInner - Id : 0
2020-05-20 10:18:32.979 31008-31162/com.example.materialdesigntest W/erialdesigntes: Accessing hidden method Lcom/android/org/conscrypt/OpenSSLSocketImpl;->getAlpnSelectedProtocol()[B (greylist,core-platform-api, reflection, allowed)
2020-05-20 10:18:33.103 31008-31162/? E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: com.example.materialdesigntest, PID: 31008
kotlin.NotImplementedError: An operation is not implemented: Not yet implemented
at com.example.materialdesigntest.TitleFragment$onViewCreated$1.onResponse(TitleFragment.kt:50)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:206)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
2020-05-20 10:18:33.182 31008-31008/? D/ViewRootImpl@7fbfe7a[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 0 1
2020-05-20 10:18:33.182 31008-31008/? D/InputMethodManager: prepareNavigationBarInfo() DecorView@76366da[MainActivity]
2020-05-20 10:18:33.182 31008-31008/? D/InputMethodManager: getNavigationBarColor() -855310
2020-05-20 10:18:33.197 31008-31162/? I/Process: Sending signal. PID: 31008 SIG: 9
谢谢
更新:我删除了TODO注释,但仍提供以下错误日志:
2020-05-20 10:34:47.165 6428-6515/com.example.materialdesigntest E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: com.example.materialdesigntest, PID: 6428
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:9812)
at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1841)
at android.view.View.requestLayout(View.java:26338)
at android.view.View.requestLayout(View.java:26338)
at android.view.View.requestLayout(View.java:26338)
at android.view.View.requestLayout(View.java:26338)
at android.view.View.requestLayout(View.java:26338)
at android.view.View.requestLayout(View.java:26338)
at androidx.constraintlayout.widget.ConstraintLayout.requestLayout(ConstraintLayout.java:3172)
at android.view.View.requestLayout(View.java:26338)
at androidx.constraintlayout.widget.ConstraintLayout.requestLayout(ConstraintLayout.java:3172)
at android.view.View.requestLayout(View.java:26338)
at android.view.View.requestLayout(View.java:26338)
at androidx.constraintlayout.widget.ConstraintLayout.requestLayout(ConstraintLayout.java:3172)
at android.view.View.requestLayout(View.java:26338)
at android.widget.TextView.checkForRelayout(TextView.java:10760)
at android.widget.TextView.setText(TextView.java:6830)
at android.widget.TextView.setText(TextView.java:6630)
at android.widget.TextView.setText(TextView.java:6582)
at com.example.materialdesigntest.TitleFragment$onViewCreated$1.onResponse(TitleFragment.kt:48)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:206)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
更新2-让它工作 默认情况下TODO()
会引发异常,因此请尝试删除以下行:TODO(“尚未实现”)
下一个错误:您只能从主线程更改视图。因此,不是:
override-fun-onResponse(调用:调用,响应:响应){
endText.text=response.body.toString()
}
您必须将作业发布到主线程,如下所示:
override-fun-onResponse(调用:调用,响应:响应){
require().runOnUiThread{
endText.text=response.body.toString()
}
}
不幸的是,变化不大,我的应用程序仍然崩溃。它现在可以工作了,但我的结果是:okhttp3.internal.http。RealResponseBody@62c589a. 我如何才能将其更改为站点的实际数据?@RohitKarthik您必须解析响应,例如使用Gson来访问您需要的数据