Nested Volley Requests in Android Studio in single session connection - android

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

Related

Appending string at the end of url in GET method :Volley

I am working with volley library in my android app development.
I have a base url and I need to append some value at the end of the url,click here,
So, this value "ZGxb87HuJK" keeps changing dynamically in my program and need to append this value at the end of url. How to add this in params?
Use this way.
StringRequest strreq = new StringRequest(Request.Method.GET,
"https://sample.com/testing/" + Hear Your dynamic value,
new Response.Listener<String>() {
#Override
public void onResponse(String Response) {
// get response
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError e) {
e.printStackTrace();
}
});
Volley.getInstance(this).addToRequestQueue(strreq);
String URL = "https://sample.com/testing/" + "dynamic value e.g ZGxb87HuJK";
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
URL, null,
new Response.Listener() {
#Override
public void onResponse(JSONObject response) {
//Success Callback
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//Failure Callback
}
});
// Adding the request to the queue along with a unique string tag
MyApplication.getInstance().addToRequestQueue(jsonObjectReq, "getRequest");
Change your code like this

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)

how to pass two Strings in "StringRequest" in android

i want to get data from two different tables in MYSQL so i used "StringRequest" method to retrieve data from MYSQL, In the below code i can get data from one table and view it in ListView in android but how i can change my code so that i will get data from another table too.
here is my code:
String url ="http://alwaysready.16mb.com/OnlineJobSort.php;";
String url_lock="http://alwaysready.16mb.com/LocalSort.php?";
StringRequest stringRequest = new StringRequest(URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
showJSON(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Jobs.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
From the above code i can able to get data from "url" but i need to get data from both "url"&"url_lock".
Well, it's simple. You just need to make 2 StringRequests with different URL parameter
String url = "http://alwaysready.16mb.com/OnlineJobSort.php;";
String url_lock = "http://alwaysready.16mb.com/LocalSort.php?";
StringRequest stringRequest1 = new StringRequest(url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
showJSON(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Jobs.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});
StringRequest stringRequest2 = new StringRequest(url_lock, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
showJSON(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Jobs.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest1);
requestQueue.add(stringRequest2);

Another Volley request inside the callback listener of Volley request is not working in Android

I am developing an Android app. In my app I need to connect with server. So I am using Volley for it. But I have a little problem with Volley. Here is my code:
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, param, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
secondRequest();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
VolleySingleton.getInstance(getBaseContext()).addToRequestQueue(request);
void secondRequest()
{
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, param, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
VolleySingleton.getInstance(getBaseContext()).addToRequestQueue(request)
}
In my code above, the second request is not working. But the first request is working. My question is, Volley can be used to do the request like this? I mean another request inside the response of a request?

Volley pass a response outside of the class

I want to pass a response outside of my classes (many classes)
public static void userLocation()
{
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
//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);
}
Please suggest how I can pass a response outside of the class
In volly its difficult to return response outside the class.because request on server run in background and if we return value followed by queue.add(url) then it will return null.So there is no solution till now.Thanks!!

Categories

Resources