packagecom.example.takahiro.simpleapplicationimportandroid.os.AsyncTask...importokhttp3.OkHttpClientimportokhttp3.Requestimportokhttp3.ResponseclassMainActivity:AppCompatActivity(){internalvarclient=OkHttpClient()overridefunonCreate(savedInstanceState:Bundle?){super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)valtoolbar=findViewById(R.id.toolbar)asToolbarsetSupportActionBar(toolbar)<省略>// GET Weatherボタンを取得して、クリック処理を実装valbtn=findViewById(R.id.button)asButtonbtn.setOnClickListener{object:MyAsyncTask(){overridefundoInBackground(varargparams:Void):String{varres:String=""try{res=run("http://api.openweathermap.org/data/2.5/weather?APPID=<自身のAPPID>&q=Tokyo")valresJson=JsonObject(res)valweathers=resJson.getJSONArray("weather")valweather=weathers.getJsonObject(0)valdescription=weather.getString("description")Log.i("MainActivity",description)res=description}catch(e:IOException){e.printStackTrace()}catch(e:JSONException){e.printStackTrace()}}}.execute()}}funrun(url:String):String{valrequest=Request.Builder().url(url).build()valresponse=client.newCall(request).execute()returnresponse.body().string()}}