How to get first value of array using Volley in android - 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);
}

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

Volley priority doesn't work properly

I have to make two calls with volley. My problem is that I need to be done the first call and then the second. The calls are on a for loop. So I put on the first call Priority.IMMEDIATE and on the second Priority.LOW. But sometimes the second call is done before the first and I doesn't have the data that I need from the first call. What am I missing?
for (int i = 0; i < SiteData.getSites().size(); i++) {
firstCall();
secondCall();
}
the firstCall method
private void firstCall(){
JsonObjectRequest siteDataRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
// do something with json
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(membership_id_tag, error.getMessage());
}
})
{
#Override
public Priority getPriority() {
return Priority.IMMEDIATE;
}
};
AppController.getInstance().addToRequestQueue(siteDataRequest, membership_id_tag);
}
the secondCall method
private void secondCall(){
JsonArrayRequest pagesRequest = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
// do something with json
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(membership_page_tag, error.getMessage());
}
})
{
#Override
public Priority getPriority() {
return Priority.LOW;
}
};
AppController.getInstance().addToRequestQueue(pagesRequest, membership_page_tag);
}
Do I have to make the first call on a loop and the second on other?
Try doing the serving call in the response if the first, also the priority might be happening because there are 2 different request queue that are being used.

Simultaneous volley request always returns Server error

I am trying to develop an android application in which I am hitting the server with an API using volley. I have made a singleton RequestQueue object and sending multiple JSON request one after other. When I send the first request, I receive the JSON object without any error. I am then parsing the JSON, extracting some ids and again making a url and making a volley request. This time, I am always getting "com.android.volley.ServerError". Below is my code:
JsonObjectRequest jReq = new JsonObjectRequest(Request.Method.GET, url, (String) null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject jsonObject) {
final VolleyParser vp = new VolleyParser();
ArrayList<Integer> searchIds = vp.parseJSONObject(jsonObject, kf, null);
r.cancelAll("saz");
for(Integer id : searchIds) {
final Fetch pf = new ProductFetch();
String url = pf.searchURL(id.toString());
JsonObjectRequest jReqNext = new JsonObjectRequest(Request.Method.GET, url, (String) null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject jsonArray) {
Log.i("saz", "product rec");
vp.parseJSONObject(jsonArray, pf, null);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
Log.i("saz", "pro "+volleyError.toString());
}
});
/**//*(Request.Method.POST, url, (String) null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject jsonObject) {
Log.i("saz","product rec");
vp.parseJSONObject(jsonObject, pf, null);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
Log.i("saz", volleyError.toString());
}
});*/
jReqNext.setRetryPolicy(new DefaultRetryPolicy(
9000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
r.add(jReqNext);
}
}}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
Log.i("saz", "Search "+volleyError.toString());
}
});
r.add(jReq);
}
Please give more explanation what the server error is but it looks like that you are getting the Volley Server Timeout error. Please increase the request timeout to one min and see the results. Increase the timeout as
yourRequest.setRetryPolicy(new DefaultRetryPolicy(60000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

Android: how to do a simple POST request with Volley, without expected result?

I want to send a POST request to my server, and there is no expected data for the result (just HTTP status code - standard behavior). How can I do that ?
(abstract base Request class (Volley) wants a result type)
try {
mRequest =
new XXXXXX(
Request.Method.POST,
url,
null, null,
new Response.Listener() {
#Override
public void onResponse() {
// ok
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError e) {
// ko
}
},
null
);
return mRestCoreVolley.addToRequestQueue(mRequest);
} catch (Exception e) {
// error
}
You could try something like in the code below for the response listener:
new Response.Listener<Void>() {
#Override
public void onResponse(Void response) {
}
}
I guess your code is right. You can use a String like:
RequestQueue rq = Volley.newRequestQueue(this);
StringRequest postReq = new StringRequest(Request.Method.POST,
your_url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
// do nothing
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// Handle error
}
}) ;
Hope it helps you!

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