Well long story short i face this kind of problem, Im trying to send volley request to my server, which responds with json array:
{
"images": [{
"product_serial_num": "1",
"product_title": "Abbadon",
"product_img": "http://1.2.3.4/android/uploads/1.jpg",
"product_price": "750",
"product_description": "The destroyer"
}]
}
but no matter what i tried i still get Volley response error, my volley request:
requestQueue = Volley.newRequestQueue(getActivity());
//JsonArrayRequest of volley
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(MY_URL ,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
//parseData to parse the json response
parseData(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//If an error occurs that means end of the list has reached
Toast.makeText(getActivity(), "No More Items Available", Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(jsonArrayRequest);
the weird thing that with volley string request it does work, only the array mess things for me.
EDIT:
how ever this way i do get my array using feed:
private void getData() {
//Adding the method to the queue by calling the method getDataFromServer
requestQueue.add(getDataFromServer(requestCount));
//Incrementing the request counter
requestCount++;
}
//Request to get json from server we are passing an integer here
//This integer will used to specify the page number for the request ?page = requestcount
//This method would return a JsonArrayRequest that will be added to the request queue
private JsonArrayRequest getDataFromServer(int requestCount) {
//JsonArrayRequest of volley
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(URL_INDEX + String.valueOf(requestCount),
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
//Calling method parseData to parse the json response
parseData(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//If an error occurs that means end of the list has reached
Toast.makeText(getActivity(), "No More Items Available", Toast.LENGTH_SHORT).show();
}
});
//Returning the request
return jsonArrayRequest;
}
Thank you!
Use this code, Change JsonArrayRequest to JsonObjectRequest
JsonObjectRequest jsonArrayRequest = new JsonObjectRequest(Request.Method.GET, MY_URL, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
//parseData to parse the json response
parseData(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//If an error occurs that means end of the list has reached
Toast.makeText(getActivity(), "No More Items Available", Toast.LENGTH_SHORT).show();
}
});
Array Response
"images": [{
"product_serial_num": "1",
"product_title": "Abbadon",
"product_img": "http://1.2.3.4/android/uploads/1.jpg",
"product_price": "750",
"product_description": "The destroyer"
}]
Object Response
{
"images": [{
"product_serial_num": "1",
"product_title": "Abbadon",
"product_img": "http://1.2.3.4/android/uploads/1.jpg",
"product_price": "750",
"product_description": "The destroyer"
}]
}
Related
I need help.
I use volley for sending json object to my rest API server. And i get data from this API to my app (json). Its work fine:
JsonObjectRequest mJsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, JSONDATA, JSONListener, errorListener)
...
And now I want to send request without JSONDATA (i can't set null). It is for global values. There is not necessary send some data. And i dunno how to send this request. Can you help me?
till i understand ur problem my answer is this
StringRequest distRequest=new StringRequest(Request.Method.POST, YOUR_URL, new Response.Listener<String>() {
#Override public void onResponse(String response) {
Toast.makeText(MainActivity.this, " "+response.toString, Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(MainActivity.this, " "+error.toString, Toast.LENGTH_SHORT).show();
}
});
RequestQueue distQueue=Volley.newRequestQueue(this);
distQueue.add(distRequest);
}
Try this:
// prepare the Request
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>()
{
#Override
public void onResponse(JSONObject response) {
// display response
Log.d("Response", response.toString());
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
Log.d("Error.Response", response);
}
}
);
// add it to the RequestQueue
queue.add(getRequest);
Im trying to make a post request with volley.
The request parameter is a json array .
Following is my request parameter
[
"Type",
{
"User": "email",
"password": "dimmer",
}
]
I have a method frameJsonArray that frames the above json and im making post request as follows,
JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(Request.Method.POST, Constants.requestUrl, frameJsonArray(),
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_SHORT).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
hideDialog();
error.printStackTrace();
}
}
);
Im getting error in the above line of code where im making the request. How can I get this sorted?
Following is my error log
Error:(162, 46) error: constructor JsonArrayRequest in class JsonArrayRequest cannot be applied to given types;
required: String,Listener<JSONArray>,ErrorListener
found: int,String,JSONArray,<anonymous Listener<JSONObject>>,<anonymous ErrorListener>
reason: actual and formal argument lists differ in length
Following is my frameJsonArrayMethod
public JSONArray frameJsonArray() {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("login_type", "Android");
jsonObject.put("username", email);
jsonObject.put("password", password);
jsonObject.put("short_name", null);
jsonObject.put("ip","123.421.12.21");
} catch (JSONException e) {
e.printStackTrace();
}
JSONArray jsonArray = new JSONArray();
jsonArray.put("Login");
jsonArray.put(jsonObject);
Log.d(TAG, "he;ll " + jsonArray.toString());
Toast.makeText(getApplicationContext(), jsonArray.toString(), Toast.LENGTH_SHORT).show();
return jsonArray;
}
JsonArrayRequest req = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
pDialog.hide();
}
});
Source : http://www.androidhive.info/2014/05/android-working-with-volley-library-1/
once check your JSON , wrong format , you missed double quote.
[
"Type",
{
"User": "email /** you missed double quote here **/,
"password": "dimmer",
}
]
JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(Request.Method.POST, Constants.requestUrl, frameJsonArray(),
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_SHORT).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
hideDialog();
error.printStackTrace();
}
}
);
Change JSONObject to JSONArray in onResponse()
EDIT The error coming because there is only one constructor in JsonArrayRequest i.e.
public JsonArrayRequest(String url, Listener<JSONArray> listener, ErrorListener errorListener){}
source https://android.googlesource.com/platform/frameworks/volley/+/e7cdf98078bc94a2e430d9edef7e9b01250765ac/src/com/android/volley/toolbox/JsonArrayRequest.java
you are using some constructor with 5 arguments.
I have to make json array request with the following request paramatetrs.
[
"Login",
{
"password": "",
"username": "",
"ip": "12.123.124.12",
"login_type": "Android"
}
]
I use volley to make post request. In volley, if we have to make jsonarrayrequest, we do something like the following,
JsonArrayRequest req = new JsonArrayRequest(Constants.requestUrl,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
pDialog.hide();
}
});
The problem is that, How can I ever be able to insert my request parameters in the above code snippet. In case of JsonObjectRequest, we have provision to insert as follows,
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, Constants.requestUrl, frameLoginJson(),
new Response.
Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
hideDialog();
error.printStackTrace();
}
}
);
In frameLoginJson, im framing the request parameter and dispatching the request.
But Im unable to do the same in case of JSONArrayRequest. How can I be able to make json array request with request parameters using volley especially or by any other mean?
You would need to override the getParams method with the following signature: protected Map<String,String> getParams().
Here's an example:
see the answer to Volley not calling getParams() for standard POST request
I have the following JSON :
{
"user_wallets": [
{
"user_id": "56",
"wallet_id": "25",
"wallet_name": "Dandora Youth Voucher",
"balance": "1,150.00"
},
{
"user_id": "56",
"wallet_id": "36",
"wallet_name": "Pfizer Chama",
"balance": "0.00"
},
{
"user_id": "56",
"wallet_id": "37",
"wallet_name": "Sunshine",
"balance": "1,000.00"
}
]
}
I want to add the wallet_name and balance to a list view like this:
wallet_name balance
wallet_name balance
wallet_name balance
and so on. However, I make the http call to my api using volley library but the response shows and empty Toast message.
Where can I be wrong?
The call I am making is this :
public void getMyWallets(){
final JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try{
JSONArray user_wallets = response.getJSONArray("user_wallets");
for(int i = 0; i<user_wallets.length();i++){
JSONObject wallet = user_wallets.getJSONObject(i);
WalletModel walletModel = new WalletModel();
walletModel.setWallet_name(wallet.getString("wallet_name"));
walletModel.setBalance(((Number) wallet.get("balance")).doubleValue());
walletModelList.add(walletModel);
}
} catch (JSONException e) {
e.printStackTrace();
}
walletListAdapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
hidepDialog();
}
});
MyApplication.getInstance().addToRequestQueue(jsonObjReq);
}
There is a networkResponse reference in the VolleyError, check it out and see if you get any useful info in order to understand where is the bug.
Something like this:
#Override
public void onErrorResponse(VolleyError error) {
if(error.networkResponse.data!=null) {
try {
body = new String(error.networkResponse.data,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
So I figured out that there was no problem with the code. The problem was a timeout issue with volley requests:
Check out
Volley RequestQueue Timeout and https://stackoverflow.com/questions/17094718/android-volley-timeout?rq=1 . I solved my empty list problem by doing this:
RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext());
MyApplication.getInstance().addToRequestQueue(jsonObjReq);
int socketTimeout = 15000;//30 seconds - change to what you want
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
jsonObjReq.setRetryPolicy(policy);
mRequestQueue.add(jsonObjReq);
That added more request timeout for the data to be fetched :)
I'm new using Vollay and I don't know if I'm doing something wrong.
I´m trying to get a json from an url and when I request it by "Vollay" It goes to the VolleyError and the error has the value I want.
Any idea why I'm getting it like an error? and how can I fix it?
Below you can see my code.
RequestQueue queue = Volley.newRequestQueue(this.getActivity());
final ProgressDialog progressDialog = ProgressDialog.show(this.getActivity(), "Wait please","Loading..");
JsonArrayRequest req = new JsonArrayRequest(Url, new Response.Listener<JSONArray>(){
#Override
public void onResponse(JSONArray response) {
Log.e("My response", response.toString());
progressDialog.cancel();
}
}, new Response.ErrorListener(){
#Override
public void onErrorResponse(VolleyError error) {
Log.e("My response", error.toString());
progressDialog.cancel();
}
});
Thanks.
Url: [http://pipes.yahoo.com/pipes/pipe.run?_id=666721920db27c5f3d996add6cdc048b&_render=json&destino=Sevilla+Prado+S.S.&id_destino=994&id_origen=999&origen=Sanlucar+d+Barrameda]http://pipes.yahoo.com/pipes/pipe.run?_id=666721920db27c5f3d996add6cdc048b&_render=json&destino=Sevilla+Prado+S.S.&id_destino=994&id_origen=999&origen=Sanlucar+d+Barrameda
Error = com.android.volley.ParseError: org.json.JSONException: Value {"count":0,"value":{"title":"Get TimeTable Amarillos","description":"Pipes Output","link":"http://pipes.yahoo.com/pipes/pipe.info?_id=666721920db27c5f3d996add6cdc048b","pubDate":"Mon, 06 Oct 2014 18:14:20 +0000","generator":"http://pipes.yahoo.com/pipes/","callback":"","items":[]}} of type org.json.JSONObject cannot be converted to JSONArray
Problem was I was trying to get a JsonArrayRequest and the result is a JsonObje
JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, Url, null,
new Response.Listener<JSONObject>()
{
#Override
public void onResponse(JSONObject response) {
Log.e("Response", response.toString());
progressDialog.cancel();
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "headers: " + error.networkResponse.headers);
Log.e(TAG, "statusCode: " + error.networkResponse.statusCode);
progressDialog.cancel();
}
}
);
Lets look at the response:
{
"count": 0,
"value": {
"title": "Get TimeTable Amarillos",
"description": "Pipes Output",
"link": "http://pipes.yahoo.com/pipes/pipe.info?_id=666721920db27c5f3d996add6cdc048b",
"pubDate": "Mon, 06 Oct 2014 18:22:13 +0000",
"generator": "http://pipes.yahoo.com/pipes/",
"callback": "",
"items": [ ]
}
}
this is a JSONObject so you must not use JsonArrayRequest, change that to JsonObjectRequest.