String request with volley fail - android

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.

Related

What's the same in Retrofit POST like StringRequest in Volley?

For example, I have Volley code for StringRequest here.
What do we use to achieve the same result for Retrofit and how does interface look like in this case?
String url = getString(R.string.API_URL) + "/social/revoke-token";
StringRequest postRequest = new StringRequest
(Request.Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("RESPONSE FROM SERVER", response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// TODO: Handle error
}
}){

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!");
}
});

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);

Volley sending multiple requests tracking

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).

Categories

Resources