How to make a synchronous volley StringRequest? - android

How to make Volley StringRequest should wait for the one response to complete and after completing the first response it should start another request.

you have to do if your first response successfully then call these second method your answers is into your question

Inside onResponse of your StringRquest make the second request you want.
StringRequest stringRequest = new StringRequest(Request.Method.POST, recieveMessageUrl, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
//create second request here
//and add it to queue
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
Note: you will have to make sure that your response is valid otherwise the onErrorResponse will be called.

Related

Nested Volley Requests in Android Studio in single session connection

I want to extract data from an HTTPS site. but It's generating an access code for every new opening of the website. so I used volley for getting access code once and getting the result of https once but when I am making 2nd request to get the result, a new session is a creation that leads to a change of access code. Can I do this in a single request ? or is there any alternate way to do this?
RequestQueue queue = Volley.newRequestQueue(this);
String url ="https://jntukresults.edu.in/view-results-56736070.html";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
access_code=StringUtils.substringBetween(response, "&accessToken=\"+", ",true);");
Toast.makeText(getApplicationContext(),raju,Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_SHORT).show();
}
});
Volley.newRequestQueue(getApplicationContext()).add(stringRequest);
String resultUrl="https://jntukresults.edu.in/results/res.php?ht=16FE1A0593&id=56736070&accessToken="+access_code;
StringRequest stringRequest1=new StringRequest(Request.Method.GET, resultUrl, new Response.Listener<String>() {
#Override
public void onResponse(String response2) {
textView.setText(response2);
Toast.makeText(getApplicationContext(),response2,Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
Volley.newRequestQueue(getApplicationContext()).add(stringRequest1);

Get volley request tag in response

I'm using volley and I have a queue to call some APIs. The queue is filled from a database.
before adding request to volley request queue I set request tag by calling
jsonObjectRequest.setTag(id);
In response, I want to remove a column from the database that column id is equal to request tag id.
So, How can I get request tag in HttpRequest response?
First create a Listener that give response from your volly class
/** Callback interface for delivering parsed responses. */
public interface Listener {
/** Called when a response is received. */
public void onResponse(Object tag, JSONObject response);
public void onErrorResponse(Object tag, VolleyError error);
}
And now create method as below where you pass listener and tag and call volly request. in response you able get tag and response at same time.
public void callApi(String url, final Listener listener, final Object tag){
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
listener.onResponse(tag,response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
listener.onErrorResponse(tag,error);
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq);
}
Its just sample code, You can modify on your requirement. If you need any help comment.

Pass details to volley inner class

This answer should be simple but I need help.
I add a url call to Volley and on error response I need to know what the URL was that the error is for, I may have 50 different URL's in a single queue for example so I want to know which one of the 50 returned the error.
Here is my code:
public void upload(String passurl) {
StringRequest stringRequest;
// Request a string response
stringRequest = new StringRequest(Request.Method.GET, passurl,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d(TAG,"Send successful");
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//***I want the URL that failed here!
error.printStackTrace();
}
});
stringRequest.setRetryPolicy(new DefaultRetryPolicy(30000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
// Add the request to the queue
stringRequest.setShouldCache(false);
stringRequest.setTag(TAG);
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(this.getApplicationContext());
mRequestQueue.start();
}
mRequestQueue.getCache().clear();
mRequestQueue.add(stringRequest);
}
To expand on #Levon's answer make passurl final for example method(final String passurl)

Send a text(or string) to server and get a response

I want to make a register/login app. I found a few examples but the most of this doesn't work for me. So I will do everything step by step. So first for the login I want to send the Server the username and the password of the user. So here is everything working, but as you can see I didn't send anything to the Server. So, where and how to send the "username" and "password" to the Server.
private void userLogin() {
username = usernameField.getText().toString().trim();
password = passwordField.getText().toString().trim();
RequestQueue queue = Volley.newRequestQueue(this);
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, LOGIN_URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast toast = Toast.makeText(getApplicationContext(), (response), Toast.LENGTH_SHORT);
toast.show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast toast = Toast.makeText(getApplicationContext(), "Anything doesn't work!", Toast.LENGTH_SHORT);
toast.show();
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
}
try the following got if your request is GET request.
private void userLogin() {
username = usernameField.getText().toString().trim();
password = passwordField.getText().toString().trim();
RequestQueue queue = Volley.newRequestQueue(this);
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, LOGIN_URL+"?username="+username+"&password="+password, // here you have to add parameters of webservice
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast toast = Toast.makeText(getApplicationContext(), (response), Toast.LENGTH_SHORT);
toast.show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast toast = Toast.makeText(getApplicationContext(), "Anything doesn't work!", Toast.LENGTH_SHORT);
toast.show();
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
}
You can't send Data with a GET request HTTP body.
For that, you use a POST request.
Then, look at the other StringRequest constructors for the one that has more than 4 parameters for (method, url, onSuccess, onFailure).
OR... I assume you are using a JSON REST API, so you need a JSONObjectRequest
Technically it is not forbidden to use a body in a get request.
It is however a very bad idea. (more info here: HTTP GET with request body)
This is why many http APIs do not allow you to do it.

method return null value

Here's my layout code;
static String userLocation(String url) {
RequestQueue queue = Volley.newRequestQueue(context);
// String url = "http://www.jobdiagnosis.com/iphone/userlocation.php";
StringRequest dr = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>()
{
#Override
public void onResponse(String response) {
// response
res=response;
//edit_location.setText(response);
// Toast.makeText(context, response, Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
// error.
Toast.makeText(getApplicationContext(), "error"+error, Toast.LENGTH_LONG).show();
Log.d("error", ""+error);
}
}
);
queue.add(dr);
return res;
}
When I toast the response on same method then response prints fine
and when I call method and print return statement then res prints null.
Any help is appreciated. Thank you in advance.
Please suggest me how we can share response within a one or more activity
And sorry for my bad English..
this is an asynchronous request, the volley processes the request in background thread and delivers the response in onResponse()
if you're looking for synchronous response try this
Try to declare res like global variable. Not return value. Because you didn't know, when request will finished. And use this global variable.

Categories

Resources