嘿,开发人员,我有一个问题,在Kotlin为Android找到一种方法来获取HTTP数据,并将其插入到textView
val HTTPget = findViewById<View>(R.id.buttonPOst) as Button
HTTPget.setOnClickListener{
val t = findViewById<View>(R.id.editTextTextPersonName) as EditText
val result = URL(t.toString()).readText()
// Device 1
val net1 = findViewById<TextView>(R.id.textViewzx)
GlobalScope.launch {
val adid = result.toString()
Log.e("MainActivity","ADID="+adid)
withContext(Dispatchers.Main){
net1.setText(adid)
}
}
我认为URL(t.toString()).readText()是正确的方式,但它给了我这个错误代码:
[PatchStore] create disable file for com.thordalenterprise.httpgetter uid is 10179
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.thordalenterprise.httpgetter, PID: 8301
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1565)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:115)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:103)
at java.net.InetAddress.getAllByName(InetAddress.java:1152)
at com.android.okhttp.Dns$1.lookup(Dns.java:41)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector
at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:144)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:86)
at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:176)
atcom.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.jav
at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.jav
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionIm
com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:248)
com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:211)
com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:30)
at java.net.URL.openStream(URL.java:1072)
at kotlin.io.TextStreamsKt.readBytes(ReadWrite.kt:150)
at com.thordalenterprise.httpgetter.MainActivity.onCreate$lambda-1(MainActivity.kt:60)
at com.thordalenterprise.httpgetter.MainActivity.$r8$lambda$yqRXLCuKxvg6aIX1POsLtCj1iGk(Unknown Source:0)
at com.thordalenterprise.httpgetter.MainActivity$$ExternalSyntheticLambda0.onClick(Unknown Source:2)
at android.view.View.performClick(View.java:7192)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View.performClickInternal(View.java:7166)
at android.view.View.access$3500(View.java:824)
at android.view.View$PerformClick.run(View.java:27592)
at android.os.Handler.handleCallback(Handler.java:888)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:8178)
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:1101)
I/Process: Sending signal. PID: 8301 SIG: 9
我应该使用okhttp库吗?但是我想要一个非常简单的方法。
我认为你错过了从人名字段获取文本尝试下面的代码
val HTTPget = findViewById<View>(R.id.buttonPOst) as Button
HTTPget.setOnClickListener{
val t = findViewById<View>(R.id.editTextTextPersonName) as EditText
val result = URL(t.text.toString()).readText() //here change
// Device 1
val net1 = findViewById<TextView>(R.id.textViewzx)
GlobalScope.launch {
val adid = result.toString()
Log.e("MainActivity","ADID="+adid)
withContext(Dispatchers.Main){
net1.setText(adid)
}
}