Home > OS >  android studio volley web request
android studio volley web request

Time:12-17

I'm trying to make a get request to an API that returns a JSON array on android studio but when I check the logs it says that there was a problem... (I use kotlin by the way)

What did I do wrong?

here is my code:

val url = "http://example.com"
    val queue = Volley.newRequestQueue(context)

    val jsonArrayRequest = JsonArrayRequest(Request.Method.GET, url, null,
        {
            

            Log.d("notification", "successful request!")
        },
        {
            Log.d("notification", "error on request...")
        })

    queue.add(jsonArrayRequest)
}

CodePudding user response:

var reqParam = URLEncoder.encode("username", "UTF-8")   "="   URLEncoder.encode(userName, "UTF-8")
    reqParam  = "&"   URLEncoder.encode("password", "UTF-8")   "="   URLEncoder.encode(password, "UTF-8")

    val mURL = URL("<Yout API Link>?" reqParam)

    with(mURL.openConnection() as HttpURLConnection) {
        // optional default is GET
        requestMethod = "GET"

        println("URL : $url")
        println("Response Code : $responseCode")

        BufferedReader(InputStreamReader(inputStream)).use {
            val response = StringBuffer()

            var inputLine = it.readLine()
            while (inputLine != null) {
                response.append(inputLine)
                inputLine = it.readLine()
            }
            it.close()
            println("Response : $response")
        }
    }

Try this it will help solve your problem..

  • Related