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.
Related
How can I parse the following JSON using Android Volley?
[
{
"msg": "success",
"id": "1542",
"firstname": "Sam",
"lastname": "Benegal",
"email": "bs#gmail.com",
"mobile": "8169830000",
"appapikey ": "f82e4deb50fa3e828eea9f96df3bb531"
}
]
That looks like pretty standard JSON, so Volley's JsonObjectRequest and JsonArrayRequest request types should parse it for you. For example:
JsonArrayRequest request = new JsonArrayRequest(
Request.Method.GET,
"https://yoururl",
null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONArray response) {
JSONObject msg1 = response.getJSONObject(0);
String firstName = msg.getString("firstname") // Sam
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// TODO
}
}
);
Code example adapted from the documentation, here: https://developer.android.com/training/volley/request#request-json.
try this
StringRequest stringRequest = new StringRequest(URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONArray jsonArray1 = new JSONArray(response);
for (int i = 0; i < jsonArray1.length(); i++) {
JSONObject object = jsonArray1.getJSONObject(i);
{
Toast.makeText(this, ""+object.getString("msg")+"\n"+object.getString("id"), Toast.LENGTH_SHORT).show();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(stringRequest);
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"
}]
}
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've sent parameter to my server but in return it sent error message,
JSONObject cannot be converted to JSONArray
this is my code:
// Creating volley request obj
JsonObjectRequest taxiReq = new JsonObjectRequest (url,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
try {
JSONArray taxiJsonArray = response.getJSONArray("taxi_list");
// Parsing json
for (int i = 0; i < taxiJsonArray.length(); i++) {
JSONObject obj = taxiJsonArray.getJSONObject(i);
Taxi taxi = new Taxi();
taxi.setTaxiname(taxiJsonArray.getString("taxiname"));
taxi.setThumbnailUrl(taxiJsonArray.getString("image"));
taxi.setdeparture(taxiJsonArray.getString("departure"));
taxi.setarrive(taxiJsonArray.getString("arrive"));
taxi.setseat(taxiJsonArray.getInt("seat"));
taxi.setcost(taxiJsonArray.getInt("cost"));
// adding taxi to taxi array
taxiList.add(taxi);
}
} catch (JSONException e) {
e.printStackTrace();
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Requesting Taxi Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
}
})
and this is my json:
{
"error": false,
"taxi_list": [
{
"image": "http://localhost/androidapp/taxiprofile/1.jpg",
"taxiname": "Taxi 1",
"from": "PTK",
"to": "SGU",
"departure": "08:00:00",
"arrive": "13:00:00",
"seat": 7,
"cost": 12
},
{
"image": "http://localhost/androidapp/taxiprofile/default.jpg",
"taxiname": "Taxi 2",
"from": "PTK",
"to": "SGU",
"departure": "08:00:00",
"arrive": "13:00:00",
"seat": 2,
"cost": 15
},
{
"image": "http://localhost/androidapp/taxiprofile/2.jpg",
"taxiname": "Taxi Untung Selalu",
"from": "PTK",
"to": "SGU",
"departure": "09:00:00",
"arrive": "14:00:00",
"seat": 3,
"cost": 13
}
]
}
I've tried to change JSONObject to JSONArray but it still come with errors...
maybe because I tried to get object but there isn't one...
EDIT: new error, after I changed the code
error: incompatible types: String cannot be converted to int
in line:
taxi.setTaxiname(obj.getString("taxiname"));
any help?
you have to change your Volley request to JsonObjectRequest. So your code will be:
// Creating volley request obj
JsonObjectRequest taxiReq = new JsonObjectRequest (url,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
JSONArray taxiJsonArray = response.getJSONArray("taxi_list");
// Parsing json
for (int i = 0; i < taxiJsonArray .length(); i++) {
try {
JSONObject obj = taxiJsonArray.getJSONObject(i);
Taxi taxi = new Taxi();
taxi.setTaxiname(obj.getString("taxiname"));
taxi.setThumbnailUrl(obj.getString("image"));
taxi.setdeparture(obj.getString("departure"));
taxi.setarrive(obj.getString("arrive"));
taxi.setseat(obj.getInt("seat"));
taxi.setcost(obj.getInt("cost"));
// adding taxi to taxi array
taxiList.add(taxi);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Requesting Taxi Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
}
})
Your "JSONArray response" is not a JsonArray It is a JsonObject try this once
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
JSONArray responceArray = response.getJSONArray();
// Parsing json
for (int i = 0; i < responceArray.length(); i++) {
------------- Your code------------
}
}
You're expecting for a JSONArray, but your top level element is actually an object:
{ <--- This is a JSON Object
"error": false,
"taxi_list": [
{
"image": "http://localhost/androidapp/taxiprofile/1.jpg",
"taxiname": "Taxi 1",
"from": "PTK",
"to": "SGU",
"departure": "08:00:00",
"arrive": "13:00:00",
"seat": 7,
"cost": 12
},
You need to change this:
JsonArrayRequest taxiReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>()
To request a JsonObject instead.
Make StringRequest instead of JSONObjectRequest and use this method it will work fine.
Problems in your code:
you were taking values directly from array and not from jsonobject.
you were not checking for the response came from the server is correctly formatted in JSON or not. StringRequest will help you on that matter.
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
#Override
public void onResponse(String s) {
Log.d(TAG, response.toString());
try {
JSONObject jsonObject = new JSONObject(s);
if (jsonObject.getString("error").equals("false")) {
JSONArray taxiJsonArray = jsonObject.getJSONArray("taxi_list");
// Parsing json
for (int i = 0; i < taxiJsonArray.length(); i++) {
JSONObject obj = taxiJsonArray.getJSONObject(i);
Taxi taxi = new Taxi();
taxi.setTaxiname(obj.getString("taxiname"));
taxi.setThumbnailUrl(obj.getString("image"));
taxi.setdeparture(obj.getString("departure"));
taxi.setarrive(obj.getString("arrive"));
taxi.setseat(obj.getInt("seat"));
taxi.setcost(obj.getInt("cost"));
// adding taxi to taxi array
taxiList.add(taxi);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
Log.e(TAG, "Requesting Taxi Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
}
});
i´d like to create a search for my Android app but when I try to I just get the JSON Value along with ...JSON Object cannot be converted to JSONArray. Btw I am using Volley.
Code Sample for getting JSON:
private void getData() {
String id = editTextId.getText().toString().trim();
if (id.equals("")) {
Toast.makeText(this, R.string.i_enter_request, Toast.LENGTH_LONG).show();
return;
}
loading = ProgressDialog.show(this,getString(R.string.d_wait),getString(R.string.d_fetch),false,false);
String url = AppConfig.DATA_URL+editTextId.getText().toString().trim();
JsonArrayRequest searchReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
Toast.makeText(getApplicationContext(), "Response: " + response.toString(), Toast.LENGTH_LONG).show();
loading.dismiss();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject pObj = response.getJSONObject(i);
Products item = new Products();
item.setTitle(pObj.getString("name"));
ProductItems.add(item);
} catch (JSONException e) {
e.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(), "Error: " + error.getMessage(), Toast.LENGTH_LONG).show();
loading.dismiss();
}
});
AppController.getInstance().addToRequestQueue(searchReq);
}
And the JSON itself by searching for "1":
{
products: [
{
id: "1",
name: "Test",
price: "123",
item_desc: "Its a Test"
}],
success: 1
}
It would be great if someone could help me with it.
Thanks
This is not a JSONArray:
{products:[{id: "1", name: "Test", price: "123", item_desc: "Its a Test" } ], success: 1 }
So you're getting an error because your callback tries passing it in as a JSONArray You need to change your callback to:
#Override
public void onResponse(JSONObject response) {}
And then you need to parse the response as a JSONObject, not a JSONArray.