how to pass two Strings in "StringRequest" in android - 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);

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

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

My request queue on android studio doesn't work

so i have this requestqueue like this with 'com.android.volley:volley:1.1.1' and permission.INTERNET
public TextView txt;
public String text="";
private static final String
url="http://192.168.100.7/diari/tampil_penyakit.php";
public RequestQueue requestQueue;
public StringRequest stringRequest;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt=(TextView) findViewById(R.id.txt);
requestQueue= Volley.newRequestQueue(MainActivity.this);
stringRequest= new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
text=response;
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
text="Error";
}
});
requestQueue.add(stringRequest);
text=text+" empty";
txt.setText(text);
}
but the txt only show "empty" and i already tried the url with postman, can somebody help me
Change your onResponse to this:
stringRequest= new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
text=response;
text=text+" empty";
txt.setText(text);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
text="Error";
}
It wasn't working because what you were basically doing was declaring the variable text like text=""; then setting up your queue, after setting it up setting your textview's text with your empty variable without waiting for a response from the server.

How to get first value of array using Volley in android

How to get first value of array using Volley in android? this method is successfully for me to get all value from array, but i just want to get first value only.
private void getValue(PriceModel priceModel, final ViewHolderPrice holder) {
StringRequest request = new StringRequest(
Request.Method.GET,
ServerApi.URL + priceModel.get_id(),
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
if(!response.isEmpty()) {
Gson gson = new Gson();
PriceModel[] priceModels = gson.fromJson(response, PriceModel[].class); for(PriceModel price: priceModels) holder.tvPrice.setText(variantModels.getPrice());
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, error.getLocalizedMessage());
}
}
);
Volley.newRequestQueue(mContext).add(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