Ciao a tutti,
sto iniziando a studiare kotlin per realizzare applicazioni android
Oggi sto cercando di collegarmi ad un API per prendere delle informazioni
L'api é gratuita.
Per il codice ho seguito un tutorial ma purtroppo non capisco l'errore, perché nonostante abbia messo un infinita di breakpoint come richiama il metodo asincorono va in errore
codice:
package com.test.apimeteo
import android.os.AsyncTask
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*
import org.json.JSONObject
import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.lang.Exception
import java.net.HttpURLConnection
import java.net.URL
import kotlin.math.log
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun GetSunset(view: View){
val appid="19cfcc9079d94b33fc8c188b4b5bb622"
var city = "london" //txtCity2.text.toString()
val url = "https://api.openweathermap.org/data/2.5/weather?q=$city&appid=$appid"
MyAsyncTask().execute(url)
}
inner class MyAsyncTask: AsyncTask<String, String, String>() {
override fun doInBackground(vararg params: String?): String {
try {
var url = URL(params[0])
var urlConnect = url.openConnection() as HttpURLConnection
urlConnect.connectTimeout =7000
var inString = ConvertStreamToString(urlConnect.inputStream)
//txtResult
publishProgress(inString)
}
catch (ex: Exception)
{
Log.e("ERRORMyAsyncTask", ex.message.toString())
}
return " "
}
override fun onPreExecute() {
//
}
override fun onProgressUpdate(vararg values: String?) {
// super.onProgressUpdate(*values)
try {
var json = JSONObject(values[0])
var coord = json.getJSONObject("coord")
var meteo = coord.getJSONObject("results")
var description = meteo.getJSONObject("description")
txtResult.text ="" +description
}
catch (ex: Exception){
Log.e("ERRORonProgressUpdate", ex.message.toString())
}
}
override fun onPostExecute(result: String?) {
//after task done
}
}
fun ConvertStreamToString(inputStream: InputStream):String {
val bufferReader = BufferedReader(InputStreamReader(inputStream))
var line: String
var allString:String=""
try {
do {
line= bufferReader.readLine()
if ( line != null){
allString += line
}
}while ( line != null)
inputStream.close()
}
catch (ex: Exception)
{
Log.e("ERRORConvertStreamTo", ex.message.toString())
}
return allString
}
}
praticamente io arrivo a MyAsyncTask().execute(url)
e mi va in errore finendo direttamente qui
codice:
public void onClick(@NonNull View v) {
if (mResolvedMethod == null) {
resolveMethod(mHostView.getContext(), mMethodName);
}
try {
mResolvedMethod.invoke(mResolvedContext, v);
} catch (IllegalAccessException e) {
throw new IllegalStateException(
"Could not execute non-public method for android:onClick", e);
} catch (InvocationTargetException e) {
throw new IllegalStateException(
"Could not execute method for android:onClick", e);
}
}
nella classe AppCompatViewInflater di androidx.appcompat.app;
Sapete darmi qualche suggerimento per intercettare l'errore o dove ho sbagliato?
Grazie