I'm trying to get a JSON object located at "https://jsonplaceholder.typicode.com/users/1" which contains
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
}
via khttp lib`rary. According the library, the usage is very simple, with a few lines it should work, but what ever http-library I try to use, the APP crashes on the Emulator and on a real phone (LG G4, Andriod 6.0) I have quite some experience with programming, but this is the first time I'm try to create a APP using Kotlin language and Andriod-Studio..
Kind request for some support! Thank you!
I tried khttp, okhttp, kohttp library; nothing seems to work in my situation
package com.example.url2bot
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.util.Log
//import io.github.rybalkinsd.kohttp.dsl.httpGet
//import io.github.rybalkinsd.kohttp.ext.asyncHttpGet
//import io.github.rybalkinsd.kohttp.ext.httpGet
import java.io.BufferedInputStream
import java.net.URL
import java.net.URLConnection
import java.io.IOException
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.URI
import java.io.File
//import okhttp3.Response
//import okhttp3.Request
//import okhttp3.OkHttpClient
//import khttp.get
import org.json.JSONObject
class MainActivity : AppCompatActivity() {
fun get(url: String): String {
try {
val connection = URI(url).toURL().openConnection() as HttpURLConnection
connection.connect()
val text = connection.inputStream.use { it.reader().use { reader -> reader.readText() } }
return text
} catch (e: IOException) {
Log.e("Error: ", "" + e)
}
return ""
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//val OriginTextField = findViewById<TextView>(R.id.OriginTextField)
//val DestinationTextField = findViewById<TextView>(R.id.DestinationTextField)
val getUrlButton = findViewById<Button>(R.id.GetURLButton)
val textViewResult = findViewById<TextView>(R.id.textViewResult)
getUrlButton.setOnClickListener{
textViewResult.text = "Hello World"
//println(get("http://jsonplaceholder.typicode.com/users/1"))//.jsonObject.getString("origin"))
textViewResult.text = (get("http://jsonplaceholder.typicode.com/users/1"))//"Bye World!"
}
}
}
Error seen:
2019-06-20 12:53:17.768 18749-18770/system_process E/system_server: Invalid ID 0x00000000.
2019-06-20 12:53:17.867 5807-5807/? E/example.url2bo: Unknown bits set in runtime_flags: 0x8000
2019-06-20 12:53:18.720 1956-2269/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
2019-06-20 12:53:18.740 1956-2209/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
2019-06-20 12:53:32.231 18749-18774/system_process E/memtrack: Couldn't load memtrack module
2019-06-20 12:53:33.156 5807-5807/com.example.url2bot E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.url2bot, PID: 5807
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.java:178)
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)
at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)
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.java:465)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:131)
at com.example.url2bot.MainActivity.get(MainActivity.kt:31)
at com.example.url2bot.MainActivity$onCreate$1.onClick(MainActivity.kt:55)
at android.view.View.performClick(View.java:7125)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3400(View.java:801)
at android.view.View$PerformClick.run(View.java:27301)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7319)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)
2019-06-20 12:53:33.222 18749-18828/system_process E/InputDispatcher: channel '2eb72d5 com.example.url2bot/com.example.url2bot.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
2019-06-20 12:53:35.048 19173-5455/com.google.android.gms.persistent E/ctxmgr: [ProducerStatusImpl]updateStateForNewContextData: inactive, contextName=7 [CONTEXT service_id=47 ]
2019-06-20 12:53:35.186 19173-19173/com.google.android.gms.persistent E/BeaconBle: Missing BluetoothAdapter
2019-06-20 12:53:35.242 19173-19173/com.google.android.gms.persistent E/BeaconBle: Scan couldn't start for Places
2019-06-20 12:53:47.004 2092-2092/? E/netmgr: Failed to open QEMU pipe 'qemud:network': Invalid argument
2019-06-20 12:53:47.004 2092-2092/? E/netmgr: WifiForwarder unable to open QEMU pipe: Invalid argument
2019-06-20 12:53:49.529 19173-3683/com.google.android.gms.persistent E/WakeLock: GCM_HB_ALARM release without a matched acquire!
User contributions licensed under CC BY-SA 3.0