The following works with the REST api, however I am now trying to achieve the same in Android Kotlin
curl -X POST \
-H 'content-type:application/json' \
-d '{"userid":"testid","first_name":"test_first_name"}' \
http://localhost:5000/users
My attempt to call the same curl in kotlin:
fun submit_new_user(view: View) {
val textView = findViewById<TextView>(R.id.textView111)
// ...
// Instantiate the RequestQueue.
val queue = Volley.newRequestQueue(this)
//build the curl
val requestBody = "userid=testid" + "&first_name=test_first_name"
// Request a string response from the provided URL.
val url = "http://localhost:5000/users?" + requestBody
val stringRequest = StringRequest(
Request.Method.POST, url,
Response.Listener<String> { response ->
// Display the first 500 characters of the response string.
textView.text = "Response is: ${response.substring(0, 20)}"
},
Response.ErrorListener { textView.text = "That didn't work!" } )
// Add the request to the RequestQueue.
queue.add(stringRequest)
}
Also can anyone explain a few security issues
Got it working by using this solution from another question,
https://stackoverflow.com/a/40118803/2203917
val textView = findViewById<TextView>(R.id.textView111)
val url = "http://localhost:5000/users"
val sr: StringRequest = object : StringRequest(
Method.POST, url,
Response.Listener { response -> textView.text = "Response is: ${response.substring(0, 20)}" },
Response.ErrorListener { error -> textView.text = "That didn't work!" }) {
@Throws(AuthFailureError::class)
override fun getBody(): ByteArray {
val params2 = HashMap<String?, String?>()
params2["userid"] = "testid"
params2["email_address"] = "testemail@example.com"
return JSONObject(params2 as Map<*, *>).toString().toByteArray()
}
override fun getBodyContentType(): String {
return "application/json"
}
}
// Add the request to the RequestQueue.
Volley.newRequestQueue(this).add(sr)
Please comment if any security issues could be improved.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.