0

So I'm trying to learn how to get data from a website and I created a mock HTTP request in json and this is mt main class:

package com.example.weatherapp

import android.app.Dialog
import android.os.AsyncTask
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
import java.lang.StringBuilder
import java.net.HttpURLConnection
import java.net.SocketTimeoutException
import java.net.URL


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        CallAPILoginAsyncTask().execute()
    }

    private inner class CallAPILoginAsyncTask(): AsyncTask<Any, Void, String>()
    {
        override fun onPreExecute()
        {
            super.onPreExecute()
            showProgressDialog()
        }

        override fun onPostExecute(result: String?)
        {
            super.onPostExecute(result)
            cancelProgressDialog()
            Log.i("JSON RESPONSE RESULT", result.toString())
        }

        private lateinit var customeProgressDialog:Dialog

        override fun doInBackground(vararg params: Any?): String
        {
            var result: String
            var connection: HttpURLConnection? = null

            try
            {
                val url = URL("https://run.mocky.io/v3/c806a112-7be9-4be4-98a9-f020112f3bfd")
                connection = url.openConnection() as HttpURLConnection
                connection.doInput = true //get data
                connection.doOutput = true // send data

                val httpResult: Int = connection.responseCode
                if(httpResult == HttpURLConnection.HTTP_OK)
                {
                    val inputStream = connection.inputStream
                    val reader = BufferedReader(InputStreamReader(inputStream))
                    val stringBuilder = StringBuilder()
                    var line: String?
                    try {
                        while (reader.readLine().also { line = it } != null)
                        {
                            stringBuilder.append(line + "\n")
                        }
                    }catch (e:IOException) {e.printStackTrace() }
                    finally
                    {
                        try {
                            inputStream.close()
                        }catch (e: IOException){e.printStackTrace()}
                    }
                    result = stringBuilder.toString()
                }else {
                    result =  connection.responseMessage
                }


            }catch (e:SocketTimeoutException){
                result = "connection Timeout"
            }catch (e: Exception){
                result = "ERrrpr: " +e.message
            }finally {
                connection?.disconnect()
            }
            return result
        }

        private fun showProgressDialog()
        {
            customeProgressDialog = Dialog(this@MainActivity)
            customeProgressDialog.setContentView(R.layout.dialog_costum_progress)
            customeProgressDialog.show()
        }

        private fun cancelProgressDialog()
        {
            customeProgressDialog.dismiss()
        }
    }
}

I found this error in my logcat and tbh I don't really understand it so if anyone knows this could you at least explain this error? and how can I solve it because I could not find my JSON request

N A
  • 1
  • I found what was not showing my JSON text in the logcat and it was that line `Log.i("JSON RESPONSE RESULT", result.toString())` it should be `Log.i("JSON RESPONSE RESULT", result!!)` – N A Dec 13 '21 at 14:27
  • But i still dont understand what is this error tho – N A Dec 13 '21 at 14:28

0 Answers0