[英]basic4android android.os.networkonmainthreadexception error
Im trying to Contol LEDs using my android device and I'am using Basic4android for the app. 我试图使用我的android设备控制LED,而我正在使用Basic4android应用。 I got things working but everytime I press a button to turn an LED on/off.. I got this error saying "android.os.NetworkOnMainThreadException" ... This is my code:
我的工作正常,但是每次我按一个按钮打开/关闭LED时。.我收到此错误消息,提示“ android.os.NetworkOnMainThreadException”。这是我的代码:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim request As HttpRequest
Dim HttpClient1 As HttpClient
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim Button1 As Button
Dim Button2 As Button
Dim Button3 As Button
Dim Button4 As Button
Dim Button5 As Button
Dim Button6 As Button
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("main")
HttpClient1.Initialize("HttpClient1")
End Sub
Sub HttpClient1_ResponseSuccess (Response As HttpResponse, TaskId As Int)
Dim resultString As String
resultString = Response.GetString("UTF8")
End Sub
Sub HttpClient1_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
Log("Error connecting: " & Reason &" "& StatusCode)
If Response <> Null Then
Log(Response.GetString("UTF8"))
Response.Release
End If
End Sub
Sub Button6_Click
request.InitializeGet("http://192.168.0.8/?BlueOFF")
HttpClient1.Execute(request, 1)
End Sub
Sub Button5_Click
request.InitializeGet("http://192.168.0.8/?BlueON")
HttpClient1.Execute(request, 1)
End Sub
Sub Button4_Click
request.InitializeGet("http://192.168.0.8/?GreenOFF")
HttpClient1.Execute(request, 1)
End Sub
Sub Button3_Click
request.InitializeGet("http://192.168.0.8/?GreenON")
HttpClient1.Execute(request, 1)
End Sub
Sub Button2_Click
request.InitializeGet("http://192.168.0.8/?RedOFF")
HttpClient1.Execute(request, 1)
End Sub
Sub Button1_Click
request.InitializeGet("http://192.168.0.8/?RedON")
HttpClient1.Execute(request, 1)
End Sub
I've researched this issue and it says that I'am doing a network operation on the main thread... I'm new to basic4android.. any tips on how to do the network operations an a separate thread? 我研究了这个问题,并说我正在主线程上进行网络操作...我是basic4android的新手。.关于如何在单独的线程上进行网络操作的任何技巧? any help is really appreciated.. thank you :D
任何帮助都非常感谢..谢谢:D
best regards, Caldwell D. 最好的问候,Caldwell D.
There are two solutions. 有两种解决方案。 The simplest would be to disable the strict mode on Android version greater API 9:
最简单的方法是在Android版本更大的API 9上禁用严格模式:
Sub DisableStrictMode
Dim jo As JavaObject
jo.InitializeStatic("android.os.Build.VERSION")
If jo.GetField("SDK_INT") > 9 Then
Dim policy As JavaObject
policy = policy.InitializeNewInstance("android.os.StrictMode.ThreadPolicy.Builder", Null)
policy = policy.RunMethodJO("permitAll", Null).RunMethodJO("build", Null)
Dim sm As JavaObject
sm.InitializeStatic("android.os.StrictMode").RunMethod("setThreadPolicy", Array(policy))
End If
End Sub
Better solution would be to replace the Response.GetString() calls (which are deprecated) with the Asynchronous Response.GetAsynchronously() calls. 更好的解决方案是用Asynchronous Response.GetAsynchronously()调用替换Response.GetString()调用(已弃用)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.