2

I'm using Volley to interact with an API. I need to send a post request with parameters to a service that returns a JSONArray.

I'm overriding the method protected Map<String, String> getParams() but it is not working:

JsonArrayRequest eventoReq = new JsonArrayRequest(Configuracion.URL_API_PROXIMOS_EVENTOS,
 new Response.Listener<JSONArray>() {
 @Override
 public void onResponse(JSONArray response) {
 Log.d(TAG, response.toString());
 hidePDialog();
 // Parsea json
 for (int i = 0; i < response.length(); i++) {
 try {
 JSONObject obj = response.getJSONObject(i);
 Evento evento = new Evento();
 evento.setCod_evento(obj.getInt("cod_evento"));
 evento.setTitulo(obj.getString("titulo"));
 evento.setDescripcion(obj.getString("descripcion"));
 evento.setDireccion(obj.getString("direccion"));
 evento.setImagen(obj.getString("imagen"));
 evento.setPuntuacion((float) obj.getDouble("puntuacion"));
 // Añade el evento al listado
 listaEventos.add(evento);
 } catch (JSONException e) {
 e.printStackTrace();
 }
 }
 // Actualiza el adaptador
 adapter.notifyDataSetChanged();
 }
 }, new Response.ErrorListener() {
 @Override
 public void onErrorResponse(VolleyError error) {
 VolleyLog.d(TAG, "Error: " + error.getMessage());
 hidePDialog();
 }
 }){
 @Override
 protected Map<String, String> getParams() {
 // Posting parameters to login url
 Map<String, String> params = new HashMap<String, String>();
 params.put("tag", "eventos_proximos_usuario");
 params.put("cod_usuario", cod_usuario);
 return params;
 }
 @Override
 public int getMethod() {
 return Method.POST;
 }
 };
 // Añade la peticion a la cola
 AppController.getInstance().addToRequestQueue(eventoReq);
Vishwajit Palankar
3,1133 gold badges30 silver badges48 bronze badges
asked Jun 1, 2015 at 16:29
2
  • what do you mean by not working? Commented Jun 1, 2015 at 18:00
  • the API don't receive POST parameters Commented Jun 1, 2015 at 18:04

4 Answers 4

8

Try ... StringRequest instead..

Reference: https://android.googlesource.com/platform/frameworks/volley/+/master/src/main/java/com/android/volley/toolbox

JsonObjectRequest extends JsonRequest

whereas StringRequest extends Request

StringRequest eventoReq = new StringRequest(Request.Method.POST,Configuracion.URL_API_PROXIMOS_EVENTOS,
 new Response.Listener<String>() {
 @Override
 public void onResponse(String response) {
 Log.d(TAG, response.toString());
 hidePDialog();
try{
 JSONArray j= new JSONArray(response);
 // Parsea json
 for (int i = 0; i < j.length(); i++) {
 try {
 JSONObject obj = j.getJSONObject(i);
 Evento evento = new Evento();
 evento.setCod_evento(obj.getInt("cod_evento"));
 evento.setTitulo(obj.getString("titulo"));
 evento.setDescripcion(obj.getString("descripcion"));
 evento.setDireccion(obj.getString("direccion"));
 evento.setImagen(obj.getString("imagen"));
 evento.setPuntuacion((float) obj.getDouble("puntuacion"));
 // Añade el evento al listado
 listaEventos.add(evento);
 } catch (JSONException e) {
 e.printStackTrace();
 }
 }
// Actualiza el adaptador
 adapter.notifyDataSetChanged();
} catch (JSONException e) {
 e.printStackTrace();
 }
 }
 }, new Response.ErrorListener() {
 @Override
 public void onErrorResponse(VolleyError error) {
 VolleyLog.d(TAG, "Error: " + error.getMessage());
 hidePDialog();
 }
 }){
 @Override
 protected Map<String, String> getParams() {
 // Posting parameters to login url
 Map<String, String> params = new HashMap<String, String>();
 params.put("tag", "eventos_proximos_usuario");
 params.put("cod_usuario", cod_usuario);
 return params;
 }
 };
 // Añade la peticion a la cola
 AppController.getInstance().addToRequestQueue(eventoReq);

Me just a beginner hope it helps!!!

answered Nov 22, 2016 at 10:42
Sign up to request clarification or add additional context in comments.

Comments

1

try like this, I think you don't need to override the getMethod() method.

Pass the method type (i.e Method.POST) name with in the JsonObjectRequest or JsonArrayRequest like below.

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
 url, null,
 new Response.Listener<JSONObject>() {
 @Override
 public void onResponse(JSONObject response) {
 Log.d(TAG, response.toString());
 pDialog.hide();
 }
 }, new Response.ErrorListener() {
 @Override
 public void onErrorResponse(VolleyError error) {
 VolleyLog.d(TAG, "Error: " + error.getMessage());
 pDialog.hide();
 }
 }) {
 @Override
 protected Map<String, String> getParams() {
 // Posting parameters to login url
 Map<String, String> params = new HashMap<String, String>();
 params.put("tag", "eventos_proximos_usuario");
 params.put("cod_usuario", cod_usuario);
 return params;
 }
 };
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
answered Jun 1, 2015 at 18:13

1 Comment

I'm trying to do the same so I implemented a custom Request but I still can't get it to work... This is my attempt: stackoverflow.com/questions/35971990/… As for your answer, it triggers the onErrorResponse...
1

I faced the same problem when I started to use Volley in my android project. I solved the problem by using StringRequest instead of JsonArrayRequest. for example if you want to show a ListView with some server data... Just use a simple StringRequest instead of JSONArrayRequest and implements the onResponse Methode like this :

public void onResponse(String response){
 try{
 JSONArray jsonArray=new JSONArray(response);
 for(int i=0;i<jsonArray.length();i++) {
 try {
 JSONObject jsonObject = jsonArray.getJSONObject(i);
 // do something
 } catch (JSONException e) {
 e.printStackTrace();
 } 
 }
 }catch (JSONException e2){
 e2.printStackTrace();
 }
 }
 }
answered Jan 5, 2017 at 14:40

Comments

0
StringRequest request = new StringRequest(Request.Method.POST, YourUrl, new Response.Listener<String>() {
 @Override
 public void onResponse(String response) {
 if (!response.equals(null)) {
 Log.e("Your Array Response", response); 
 } else {
 Log.e("Your Array Response", "Data Null");
 }
 }
 }, new Response.ErrorListener() {
 @Override
 public void onErrorResponse(VolleyError error) {
 Log.e("error is ", "" + error);
 }
 }) { 
 //This is for Headers If You Needed
 @Override
 public Map<String, String> getHeaders() throws AuthFailureError {
 Map<String, String> params = new HashMap<String, String>();
 params.put("Content-Type", "application/json; charset=UTF-8");
 params.put("token", ACCESS_TOKEN);
 return params;
 }
 //Pass Your Parameters here
 @Override
 protected Map<String, String> getParams() {
 Map<String, String> params = new HashMap<String, String>();
 params.put("User", UserName);
 params.put("Pass", PassWord);
 return params;
 }
 };
 RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
 queue.add(request);
answered May 18, 2017 at 13:14

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.