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 ?
Related
I'm trying to GET request, but I'm getting error on line 4 in the code below
val queue = Volley.newRequestQueue(this)
val url = "https://meme-api.herokuapp.com/gimme"
val jsonObjectRequest = JsonObjectRequest(
Request.Method.GET, null,
Response.Listener { response ->
val url = response.getString("url")
},
Response.ErrorListener {
Toast.makeText(this,"something wrong", Toast.LENGTH_LONG).show()
})
queue.add(jsonObjectRequest)
You havent passed url to request.
val queue = Volley.newRequestQueue(this)
val url = "https://meme-api.herokuapp.com/gimme"
val jsonObjectRequest = JsonObjectRequest(
Request.Method.GET, url, null, //You need to pass url here
Response.Listener { response ->
val url = response.getString("url")
},
Response.ErrorListener {
Toast.makeText(this,"something wrong", Toast.LENGTH_LONG).show()
})
queue.add(jsonObjectRequest)
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)
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)
}
}
The parameters should be in "param1=data1¶m2=data2¶m3=data3" format
I can send data in postmen using form-data
Any idea how i can send the data from my code?
Seriously please send help this is my second time asking
Here is the image for postman https://ibb.co/SQgG72W
You have to override getParams Method. In that you can pass your params you want to send to the server
StringRequest sr = new StringRequest(Request.Method.POST, "http://headers.jsontest.com/",
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.e("HttpClient", "success! response: " + response.toString());
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("HttpClient", "error: " + error.toString());
}
})
{
#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;
}
};
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.
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.