Reputation: 1
I am currently trying to build my first app with android studio. I am trying to access data from a webserver
My code is:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
val textView = findViewById<TextView>(R.id.xml_text_view)
val client = OkHttpClient()
val url = "http://192.168.43.41"
val request = Request.Builder().url(url).build()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
textView.text = e.toString()
e.printStackTrace()
}
override fun onResponse(call: Call, response: Response) {
if (response.isSuccessful) {
runOnUiThread {
textView.text = response.body!!.string()
}
}
}
})
}
}
When I open the app, it crashes. If I try about 3 or 4 more times, it suddenly no longer crashes and displays the data. If I change the URL to, for example: https://reqres.in/api/users?page=2 it works fine as well. If the ESP8266 web server is offline, I can open the app without a problem.
Log:
AndroidRuntime com.example.test1
E FATAL EXCEPTION: main
Process: com.example.test1, PID: 8062
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1565)
at java.net.SocketInputStream.read(SocketInputStream.java:175)
at java.net.SocketInputStream.read(SocketInputStream.java:144)
at okio.InputStreamSource.read(JvmOkio.kt:93)
at okio.AsyncTimeout$source$1.read(AsyncTimeout.kt:128)
at okio.RealBufferedSource.read(RealBufferedSource.kt:192)
at okhttp3.internal.http1.Http1ExchangeCodec$AbstractSource.read(Http1ExchangeCodec.kt:339)
at okhttp3.internal.http1.Http1ExchangeCodec$UnknownLengthSource.read(Http1ExchangeCodec.kt:475)
at okhttp3.internal.connection.Exchange$ResponseBodySource.read(Exchange.kt:281)
at okio.Buffer.writeAll(Buffer.kt:1303)
at okio.RealBufferedSource.readString(RealBufferedSource.kt:96)
at okhttp3.ResponseBody.string(ResponseBody.kt:187)
at com.example.test1.MainActivity$onCreate$2.onResponse$lambda$0(MainActivity.kt:42)
at com.example.test1.MainActivity$onCreate$2.$r8$lambda$wwUQooT56XlASn_Q0N96Hr51lHo(Unknown Source:0)
at com.example.test1.MainActivity$onCreate$2$$ExternalSyntheticLambda0.run(D8$$SyntheticClass:0)
at android.os.Handler.handleCallback(Handler.java:900)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:219)
at android.app.ActivityThread.main(ActivityThread.java:8668)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1109)
I dont know why it crashes nor why it sometimes works.
Upvotes: 0
Views: 14