我是Kotlin的新手,我想创建一个Android移动应用程序。我使用Android Studio 3.3(Android API级别28)。我用招摇的方式获取了我所有的RESTful Web服务,因此,我有了android -client生成的文件包含所有Web服务。首先,我创建了一个用于使用“电子邮件和密码”进行身份验证的android接口。在可用的Web服务中,“ apiMobileUsersGetByFireBaseIDGet”。为此,请使用Firebase ID进行身份验证。
我创建如下的kotlin类AUthentication:
class Authentication : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_authentication)
var email = editTextEmail.text
var password = editTextPassword.text
FirebaseApp.initializeApp(this@Authentication)
val auth = FirebaseAuth.getInstance()
buttonlogin.setOnClickListener {
auth.signInWithEmailAndPassword(email.toString(), password.toString())
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
println("success")
if (verifyAvailableNetwork(this@Authentication)) {
try {
CoroutineScope(Dispatchers.Main).launch {
val user = MobileApi().apiMobileUsersGetByFireBaseIDGet(auth.currentUser!!.uid)
if (user == UsersData()) {
var intent = Intent(this@Authentication, MainActivity::class.java)
intent.putExtra("id", auth.currentUser?.email)
startActivity(intent)
}
}
} catch (e: ClientException) {
println("4xx response calling apiMobileUsersGetByFireBaseIDGet")
e.printStackTrace()
} catch (e: ServerException) {
println("5xx response calling apiMobileUsersGetByFireBaseIDGet ")
e.printStackTrace()
}
}
} else {
println("Error: ${task.exception?.message}")
}
}
}
}
}
我还在AndroidManifest.xml中添加了Internet权限。 我的问题是,当我在手机中运行我的应用程序时,出现了“ NetworkOnMainThreadException”。我尝试了几次来更正此错误。我真的被卡住了。 错误说明如下:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.app.homecraft, PID: 32245
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1285)
at java.net.InetAddress.lookupHostByName(InetAddress.java:434)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:255)
at java.net.InetAddress.getAllByName(InetAddress.java:218)
at okhttp3.Dns$Companion$SYSTEM$1.lookup(Dns.kt:48)
at okhttp3.internal.connection.RouteSelector.resetNextInetSocketAddress(RouteSelector.kt:157)
at okhttp3.internal.connection.RouteSelector.nextProxy(RouteSelector.kt:122)
at okhttp3.internal.connection.RouteSelector.next(RouteSelector.kt:70)
at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:203)
at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:108)
at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:76)
at okhttp3.internal.connection.Transmitter.newExchange$okhttp(Transmitter.kt:162)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:36)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:117)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:90)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:84)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:117)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:90)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:84)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:117)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:71)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:117)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:90)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.kt:184)
at okhttp3.RealCall.execute(RealCall.kt:67)
at io.swagger.client.apis.MobileApi.apiMobileUsersGetByFireBaseIDGet(MobileApi.kt:4096)
at com.app.homecraft.Authentication$onCreate$1$1$1.invokeSuspend(Authentication.kt:38)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:233)
at android.os.Handler.handleCallback(Handler.java:743)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5639)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:689)
答案 0 :(得分:0)
您必须仅在后台流中向网络发出请求,否则当您尝试发出请求时会收到错误消息
答案 1 :(得分:0)
您的错误表示您正在UI上执行大量工作,这会使它冻结,甚至更糟。崩溃了在这种情况下,您应该将调用移到后台线程上,这意味着UI将继续正常运行,而网络请求将独立于该UI执行。由于您使用的是Kotlin coroutines
,因此您可能想将Dispatchers.Main
更改为Dispatchers.IO
。 This是一个简单的示例。
答案 2 :(得分:0)
有一行是
//TODO("Fill in more types!")
在您的 ApiClient.kt 文件中(由草率生成)。您需要更改该行以消除此异常。我不知道该用什么代替,但这是问题的根源。