Volley sending multiple requests tracking - android

I was going through volley library documentation with the following example
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
mTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
My question is if i add an other request(similar request i.e. StringRequest) request to the queue, how can i map my request to response since the response is asynchronous.
1request --> 1response.
2request --> 2response.
I don't have any priorities for request and should be done concurrently (not sequential requests).

Related

String request with volley fail

Good day, String request with volley fail error //-- Unexpected response code 404 for https://www.express.pk/world/archives/?page=1 --//
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest
("https://www.express.pk/world/archives/?page=1", new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("check", "ok");
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
neonews();
Log.d("check", "fail");
}
});
queue.add(stringRequest);
add request method GET or POST
replace your
StringRequest stringRequest = new StringRequest
("https://www.express.pk/world/archives/?page=1", new Response.Listener<String>()
with
StringRequest stringRequest = new StringRequest
(Request.Method.GET,"https://www.express.pk/world/archives/?page=1", new Response.Listener<String>()
If you are not getting success response and it goes to Error Response
check whether API have any parameter that will pass on API call
Using volley you should define specific http request type(GET, POST, PUT, DELETE).
Official doc give a good example
please modify your codes like link example.

Delete request params URL Volley android

I have an API with a delete function like this:
http://localhost/v1/deletePost/:id
when i try in postman succeed by entering param in url ":id" like ../deletePost/37".
how to implement the "/:id" request in android using Volley library?
Here is a guide about how to use Volley Library
On the page Sending a Simple Request:
final TextView mTextView = (TextView) findViewById(R.id.text);
//...
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
mTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
});
// Add the request to the RequestQueue.queue.add(stringRequest);
Then you need to change GET to DELETE Request.Method.DELETE and the url for your url with the id http://localhost/v1/deletePost/37
Will be something like this:
String baseUrl ="http://localhost/v1/deletePost/";
String url = baseUrl + "3" //Here you change the ID, can put as variable
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.DELETE, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
mTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
});

How to get JSON from server without send JSON request

I need help.
I use volley for sending json object to my rest API server. And i get data from this API to my app (json). Its work fine:
JsonObjectRequest mJsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, JSONDATA, JSONListener, errorListener)
...
And now I want to send request without JSONDATA (i can't set null). It is for global values. There is not necessary send some data. And i dunno how to send this request. Can you help me?
till i understand ur problem my answer is this
StringRequest distRequest=new StringRequest(Request.Method.POST, YOUR_URL, new Response.Listener<String>() {
#Override public void onResponse(String response) {
Toast.makeText(MainActivity.this, " "+response.toString, Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(MainActivity.this, " "+error.toString, Toast.LENGTH_SHORT).show();
}
});
RequestQueue distQueue=Volley.newRequestQueue(this);
distQueue.add(distRequest);
}
Try this:
// prepare the Request
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>()
{
#Override
public void onResponse(JSONObject response) {
// display response
Log.d("Response", response.toString());
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
Log.d("Error.Response", response);
}
}
);
// add it to the RequestQueue
queue.add(getRequest);

Volley PUT StringRequest not sent

I am sending a StringRequest using Volley to update a single record in mysql.The url is fine when i am sending the same request from postman i am getting result but when i send the request from android its not sent. I am not getting whats the issue.I am sendin two values that too iam sending it in url.Here is the code
StringRequest req = new StringRequest(Request.Method.PUT, url,new Response.Listener<String>() {
#Override
public void onResponse(String response) {
resp = Integer.parseInt(response.toString());
hidepDialog();
if(resp == 1){
Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(),"Failure",Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});

Volley Error 500

I am trying to parse a url that i found online.
However when i try to use Volley standard way of sending a request, it keeps giving me the error of
BasicNetwork.performRequest: Unexpected response code 500 for http://www.mytransport.sg/content/mytransport/home/myconcierge/trafficcameras/jcr:content/par/cameras_slideshow.singleresult.html?cameraid=2704
However, when i try to access the url via my google chrome, it works fine without any problem.
This is my code:
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.e("RESPONSE", response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// Error handling
error.printStackTrace();
}
});
// Add the request to the queue
Volley.newRequestQueue(this).add(stringRequest);

Categories

Resources