import android.annotation.SuppressLint
import android.location.Location
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationServices
import kotlinx.android.synthetic.main.activity_main.*
import org.json.JSONObject
class MainActivity : AppCompatActivity() {
var weather_url1 = ""
var api_id1 = "030314b750cc43e7b39e503dfe37150c"
private lateinit var textView: TextView
private lateinit var fusedLocationClient: FusedLocationProviderClient
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView = findViewById(R.id.textView)
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
Log.e("lat", weather_url1)
btVar1.setOnClickListener {
Log.e("lat", "onClick")
obtainLocation()
}
}
@SuppressLint("MissingPermission")
private fun obtainLocation() {
Log.e("lat", "function")
fusedLocationClient.lastLocation
.addOnSuccessListener { location: Location? ->
Log.e("lat", weather_url1.toString())
getTemp()
}
}
fun getTemp() {
val queue = Volley.newRequestQueue(this)
val url: String = weather_url1
Log.e("lat", url)
val stringReq = StringRequest(Request.Method.GET, url,
Response.Listener<String> { response ->
Log.e("lat", response.toString())
val obj = JSONObject(response)
val arr = obj.getJSONArray("data")
Log.e("lat obj1", arr.toString())
val obj2 = arr.getJSONObject(0)
Log.e("lat obj2", obj2.toString())
textView.text = obj2.getString("temp") + " deg Celsius in " + obj2.getString("city_name")
},
Response.ErrorListener { textView!!.text = "That didn't work!" })
queue.add(stringReq)
}
}