whats the Kotlin Equivalent request.post() from python?

Viewed 191

So I'm trying to use an API from a website but inorder to use it i'll have to send my login informaton. The documentation shows me a python example on how to login.

R = requests.post('http://nova.astrometry.net/api/login', data={'request-json': 
json.dumps({"apikey": "XXXXXXXX"})})
print(R.text)

So what is the Kotlin equivalent of the above code ? In the websites documentation it states

"Note the request-json=VALUE: we’re not sending raw JSON, we’re sending the JSON-encoded string as though it were a text field called request-json."

I have attempted to use Android Volley but im not entirely sure how to use it.

private fun plateSolve(){
    val json=JSONObject(map).toString()
    Log.d(TAG,"URL:$url")
    Log.d(TAG,"Hashmap:$json")

    JSONObject(map).toString()

    val jsonObjectRequest = JsonObjectRequest(
        Request.Method.POST, url, null,
        { response ->

            try {
                Log.d(TAG,"POST Response: %s".format(response.toString()))
            }catch (e:Exception){
                Log.d(TAG,"Exception: $e")

            }

        },
        { error ->
            // TODO: Handle error
            Log.d(TAG,"There Was an Error")
            error.stackTraceToString()
        }
    )

    // Access the RequestQueue through your singleton class.
    VolleySingleton.instance?.addToRequestQueue(jsonObjectRequest)

  }

Thanks in advance

1 Answers

it's not recommended to use volley anymore for android please use retrofit as its google's recommended library,the answer for your question is too big so i will write some checkpoints to do and also i have shared a simple working example with retrofit one of my own projects on github , hopefully this helps you

retrofit link - https://square.github.io/retrofit/

  1. Insert library files in gradle
  2. create response classes
  3. create retrofit api class 4.create interface class with api calls

Github project with app using retrofit for api calls https://github.com/zaidzak9/NewsApp

Related