I need make request with Volley and kotlin my cod is
val stringReq = StringRequest(
Request.Method.GET, url,
Response.Listener<String> { response ->
var strResp = response.toString()
val jsonObj: JSONObject = JSONObject(strResp)
val jsonArray: JSONArray = jsonObj.getJSONArray("curso")
for (i in 0 until jsonArray.length()) {
var jsonInner: JSONObject = jsonArray.getJSONObject(i)
listGeral.add(jsonInner.get("interpret").toString());
listGeral2.add(jsonInner.get("titel").toString());
listGeral3.add(jsonInner.get("id").toString());
}
},
Response.ErrorListener {
})
queue.add(stringReq)
Now I need send 3 parameters for php. How do I put instruction for send parameter?
you can write the below code after Response.ErrorListener
val sr: StringRequest = object : StringRequest(Method.POST, "url",
Response.Listener { response ->
//your response
},
Response.ErrorListener { error ->
//your error
}) {
override fun getParams(): Map<String, String> {
val params: MutableMap<String, String> = HashMap()
params["user"] = "YOUR USERNAME"
params["pass"] = "YOUR PASSWORD"
return params
}
#Throws(AuthFailureError::class)
override fun getHeaders(): Map<String, String> {
val params: MutableMap<String, String> = HashMap()
params["Content-Type"] = "application/x-www-form-urlencoded"
return params
}
}
queue.add(sr)
Related
This is the code:
val requestQueue: RequestQueue = Volley.newRequestQueue(this#MainActivity)
val jsonArrayRequest = JsonArrayRequest(
Request.Method.POST,
"$domain/do_getmemes.php",
null,
Response.Listener { response ->
},
Response.ErrorListener { // Do something when error occurred
}
)
requestQueue.add(jsonArrayRequest)
and I just want to add some Parameters!
I've seen this JAVA example: https://gist.github.com/mstfldmr/f6594b2337e3633673e5
but I don't know what/where/how to add the parameters from this abomination of example.
I tried to add this part right after JsonArrayRequest():
{
#Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("user","YOUR USERNAME");
params.put("pass","YOUR PASSWORD");
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("Content-Type","application/x-www-form-urlencoded");
return params;
}
}
but it doesn't get converted to Kotlin.
I need to send some Ints and Strings
add object so you can override functions
val jsonArrayRequest = object : JsonArrayRequest(
Request.Method.POST,
"$domain/do_getmemes.php",
null,
Response.Listener { response ->
},
Response.ErrorListener { // Do something when error occurred
}
) {
override fun getBody(): ByteArray {
val parameters = HashMap<String, String>()
parameters["key"] = "value"
return JSONObject(parameters.toString()).toString().toByteArray()
}
}
class test2 : AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_test2)
connect()
}
fun connect(){
val queue = Volley.newRequestQueue(this)
val url = "https://www.example.com/Register.php"
val params = HashMap<String,String>()
params["abc"] = "parm1"
params["def"] = "parm2"
params["ghi"] = "parm3"
val stringRequest = StringRequest(Request.Method.POST, url,
Response.Listener<String> { response ->
text_test.text = "Response is: $response"
},
Response.ErrorListener { text_test.text = "That didn't work! " })
queue.add(stringRequest)
}
}
I want to send the parameters in StringRequest and not in JsonObjectRequest.
The parameters should be in "param1=data1¶m2=data2¶m3=data3" format
and UTF-8 encoded.
Any idea how i can send the data in that format ?
I have some code. In Volley code:
val queue = Volley.newRequestQueue(context)
val stringRequest = StringRequest(
Request.Method.GET,
linkTrang,
Response.Listener<String> { response ->
mTextView.text = "Response is: " + response.substring(0, 500));
},
Response.ErrorListener { })
{
}
queue.add(stringRequest)
How do I set a header called Authorization in this?
I was able to do it in Kotlin using:
val linkTrang = "YOUR URL"
val queue = Volley.newRequestQueue(this)
val stringRequest = object: StringRequest(Request.Method.GET, linkTrang,
Response.Listener<String> { response ->
Log.d("A", "Response is: " + response.substring(0,500))
},
Response.ErrorListener { })
{
override fun getHeaders(): MutableMap<String, String> {
val headers = HashMap<String, String>()
headers["Authorization"] = "Basic <<YOUR BASE64 USER:PASS>>"
return headers
}
}
queue.add(stringRequest)
It is important to use the object keyword before the construction of the request in order to be able to override the getHeaders() method.
i have a web service in this url : https://server_IP:9443/api/v1/ssids.json/
but it needs login in this page : https://server_IP:9443/login
when i make the request with the credentials in the headers , i got "Unexpected response code 401"
so how can i authenticate and get the json.
here is my code :
val request = object : StringRequest(Request.Method.GET, url, Response.Listener { response ->
if (response != null) {
Log.e("Your Array Response", response)
} else {
Log.e("Your Array Response", "Data Null")
}
}, Response.ErrorListener { error -> Log.e("error is ", "" + error) }) {
override fun getHeaders() : Map<String, String> {
val username ="admin"
val password = "admin"
val auth = "$username:$password"
var data = auth.toByteArray()
var base64 = Base64.encodeToString(data, Base64.NO_WRAP)
var headers = HashMap<String, String>()
headers.put("Authorization",base64)
headers.put("accept-language","EN")
headers.put("Content-type","application/json")
headers.put("Accept","application/json")
return headers
}
}
val queue = Volley.newRequestQueue(this)
queue.add(request)
1.I'm getting the correct response back and can view it in logcat, but I need to save that response and store it for later. I'm trying to save it in the var token, but when looking in memory token remains empty even after correctly getting the response. Any suggestions would be appreciated.
class Login : AppCompatActivity() {
var token = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
//POST data
log_login.setOnClickListener {
// "http://localhost:1842/token"
var url = "https://elimination.azurewebsites.net/token"
val que = Volley.newRequestQueue(this#Login)
val stringRequest = object : StringRequest(Request.Method.POST, url,
Response.Listener { response -> Toast.makeText(this#Login, response, Toast.LENGTH_LONG).show()
Log.e("Test", response)
//trying to set token to response
token = response},
Response.ErrorListener { error -> Toast.makeText(this#Login, error.toString(), Toast.LENGTH_LONG).show()
Log.e("Wrong", error.toString())}) {
override fun getParams(): Map<String, String> {
val params = HashMap<String, String>()
params.put("grant_type", "password")
params.put("Username", input_email.toString())
params.put("Password", input_password.toString())
return params
}
}
que.add(stringRequest)
val intent = Intent(this, Profile::class.java)
intent.putExtra("token", token) //passing token to profile intent
startActivity(intent)
}
}
}
Try moving the intent initialization to when you receive the correct response:
log_login.setOnClickListener {
// "http://localhost:1842/token"
var url = "https://elimination.azurewebsites.net/token"
val que = Volley.newRequestQueue(this#Login)
val stringRequest = object : StringRequest(Request.Method.POST, url,
Response.Listener { response -> Toast.makeText(this#Login, response, Toast.LENGTH_LONG).show()
Log.e("Test", response)
//trying to set token to response
token = response
val intent = Intent(this, Profile::class.java)
intent.putExtra("token", token)
startActivity(intent)
},
Response.ErrorListener { error -> Toast.makeText(this#Login, error.toString(), Toast.LENGTH_LONG).show()
Log.e("Wrong", error.toString())}) {
override fun getParams(): Map<String, String> {
val params = HashMap<String, String>()
params.put("grant_type", "password")
params.put("Username", input_email.toString())
params.put("Password", input_password.toString())
return params
}
}
que.add(stringRequest)
}
Or calling a method when you receive the result:
log_login.setOnClickListener {
// "http://localhost:1842/token"
var url = "https://elimination.azurewebsites.net/token"
val que = Volley.newRequestQueue(this#Login)
val stringRequest = object : StringRequest(Request.Method.POST, url,
Response.Listener { response -> Toast.makeText(this#Login, response, Toast.LENGTH_LONG).show()
Log.e("Test", response)
//trying to set token to response
token = response
openNextActivity(token);
},
Response.ErrorListener { error -> Toast.makeText(this#Login, error.toString(), Toast.LENGTH_LONG).show()
Log.e("Wrong", error.toString())}) {
override fun getParams(): Map<String, String> {
val params = HashMap<String, String>()
params.put("grant_type", "password")
params.put("Username", input_email.toString())
params.put("Password", input_password.toString())
return params
}
}
que.add(stringRequest)
}
fun openActivity(token: String){
val intent = Intent(this, Profile::class.java)
intent.putExtra("token", token)
startActivity(intent)
}
The important thing is you were trying to execute the intent initialization syncronously, but you receive the token async so you token variable was not filled when the intent was executed.