Max Barinov
Max Barinov

Reputation: 53

http request in android kotlin

I try to get JSON or something like this, using URL.readText() from java.net.url in android studio, but my app crashes.

fun ButtonClick(view:View) {
    textView.text = URL("https://www.google.com/robots.txt").readText()
}

I also added this into manifest.xml

<uses-permission android:name="android.permission.INTERNET" />

but it crashes anyway

please, help me! what am i doing wrong?. I think it's ok to get text from url, using url.readText, because some guy in this post HTTP Request in Kotlin wrote that it's simpler

crash log:

2020-08-17 23:16:04.117 2251-2251/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 2251
java.lang.IllegalStateException: Could not execute method for android:onClick
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
    at android.view.View.performClick(View.java:5637)
    at android.view.View$PerformClick.run(View.java:22429)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
    at android.view.View.performClick(View.java:5637) 
    at android.view.View$PerformClick.run(View.java:22429) 
    at android.os.Handler.handleCallback(Handler.java:751) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6119) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
 Caused by: android.os.NetworkOnMainThreadException
    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
    at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86)
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74)
    at java.net.InetAddress.getAllByName(InetAddress.java:752)
    at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
    at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:187)
    at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:156)
    at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:98)
    at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:345)
    at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:328)
    at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:246)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:457)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:405)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:243)
    at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
    at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java)
    at java.net.URL.openStream(URL.java:1057)
    at kotlin.io.TextStreamsKt.readBytes(ReadWrite.kt:150)
    at com.example.myapplication.MainActivity.ButtonClick(MainActivity.kt:25)
    at java.lang.reflect.Method.invoke(Native Method) 
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409) 
    at android.view.View.performClick(View.java:5637) 
    at android.view.View$PerformClick.run(View.java:22429) 
    at android.os.Handler.handleCallback(Handler.java:751) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6119) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

Upvotes: 0

Views: 1073

Answers (3)

AndroidDev
AndroidDev

Reputation: 21

Based on what limited info you have provided, you might be having a NetworkOnMainThreadException wrap your code in a thread for it to work

fun ButtonClick(view:View){
thread {
            val response =  URL("https://www.google.com/robots.txt").readText()
            activity?.runOnUiThread{
                textView.text = response  
            }            
        }
    }

Upvotes: 1

hosein moradi
hosein moradi

Reputation: 492

you should use from Retrofit in your codes

1- Add the following lines in the app level build.gradle file to apply Google Services to your project

dependencies {  
...  
Implementation 'org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version'  
implementation 'com.squareup.retrofit2:retrofit:2.0.0'  
implementation 'com.squareup.retrofit2:converter-gson:2.0.0'   
}  

2-Then, click “Sync Now” to set up your project.

3- Don't forget to add the following permissions in your manifest file

<uses-permission android:name="android.permission.INTERNET"/>  

4- Create an interface file named as “WeatherService.kt” and add the following lines in it

interface WeatherService {  
@GET("data/2.5/weather?")  
fun getCurrentWeatherData(@Query("lat") lat: String, @Query("lon") lon: String, 
@Query("APPID") app_id: String): Call<WeatherResponse>  
} 

5-Create a class file named as “WeatherResponse.kt” and add the following lines. Here, we used a Gson Converter and so the JSON response is automatically converted to the respective and the converter will compare the response tree with the serialized name.

class WeatherResponse {  

@SerializedName("coord")  
var coord: Coord? = null  
@SerializedName("sys")  
var sys: Sys? = null  
@SerializedName("weather")  
var weather = ArrayList<Weather>()  
@SerializedName("main")  
var main: Main? = null  
@SerializedName("wind")  
var wind: Wind? = null  
@SerializedName("rain")  
var rain: Rain? = null  
@SerializedName("clouds")  
var clouds: Clouds? = null  
@SerializedName("dt")  
var dt: Float = 0.toFloat()  
@SerializedName("id")  
var id: Int = 0  
@SerializedName("name")  
var name: String? = null  
@SerializedName("cod")  
var cod: Float = 0.toFloat()  
}  

 class Weather {  
@SerializedName("id")  
var id: Int = 0  
@SerializedName("main")  
var main: String? = null  
@SerializedName("description")  
var description: String? = null  
@SerializedName("icon")  
var icon: String? = null  
}  

class Clouds {  
@SerializedName("all")  
var all: Float = 0.toFloat()  
}  

class Rain {  
@SerializedName("3h")  
var h3: Float = 0.toFloat()  
}  

class Wind {  
@SerializedName("speed")  
var speed: Float = 0.toFloat()  
@SerializedName("deg")  
var deg: Float = 0.toFloat()  
}  

class Main {  
@SerializedName("temp")  
var temp: Float = 0.toFloat()  
@SerializedName("humidity")  
var humidity: Float = 0.toFloat()  
@SerializedName("pressure")  
var pressure: Float = 0.toFloat()  
@SerializedName("temp_min")  
var temp_min: Float = 0.toFloat()  
@SerializedName("temp_max")  
var temp_max: Float = 0.toFloat()  
}  

class Sys {  
@SerializedName("country")  
var country: String? = null  
@SerializedName("sunrise")  
var sunrise: Long = 0  
@SerializedName("sunset")  
var sunset: Long = 0  
}  

class Coord {  
@SerializedName("lon")  
var lon: Float = 0.toFloat()  
@SerializedName("lat")  
var lat: Float = 0.toFloat()  
}  

6- Then, open your Activity file and in my case, I have opened my MainActivity.kt file. Then, create a Retrofit Builder with Base URL and GsonConverterFactory.

val retrofit = Retrofit.Builder()  
            .baseUrl(BaseUrl)  
            .addConverterFactory(GsonConverterFactory.create())  
            .build() 

7-Then, create a service for Retrofit with your service interface, as shown below

val service = retrofit.create(WeatherService::class.java)    
val call = service.getCurrentWeatherData(lat, lon, AppId)   

Here, “getCurrentWeatherData” is an interface function created in the “WeatherService” interface.

8-Then, create a queue with Weather Response which is used to de-serialize the JSON output of the Open Weather API. The following will show the created queue.

     call.enqueue(object : Callback<WeatherResponse> {  
        override fun onResponse(call: Call<WeatherResponse>, response: 
          Response<WeatherResponse>) {  
            if (response.code() == 200) {  
                …  
            }  
        }  
        override fun onFailure(call: Call<WeatherResponse>, t: Throwable) {  
            …  
        }  
     })  

variables defined :

 companion object {    

    var BaseUrl = "http://api.openweathermap.org/"    
    var AppId = "2e65127e909e178d0af311a81f39948c"    
    var lat = "35"    
    var lon = "139"    
  }    

Upvotes: 0

Ahmad Mahmoudi
Ahmad Mahmoudi

Reputation: 91

I was have this problem and I wasn't fix it

but now I using Volley in android and it is really powerful and easy

I suggest you to work with Volley

Upvotes: 0

Related Questions