2017-10-11 3 views
0

enter image description hereComment envoyer arraylist d'objets en paramètre à un tableau json contenant différents champs en utilisant volley à une URL dans android?

Le tableau JSON en facteur contenant product_id, unit_price et quantité indiquée dans le tableau.

Je rencontre un problème avec l'affichage de ArrayList d'objets en tant que paramètre pour remplir correctement les valeurs par rapport aux champs respectifs mentionnés dans le tableau JSON. S'il vous plaît quelqu'un qui peut résoudre mon problème? enter image description here

Ci-dessous mon code java: PlaceOrder public void (finale ArrayList productArrayList) {

btnPlaceOrder.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      String urlPlaceOrder = "http://radial-energy.com/radial/api/place-order"; 

      StringRequest stringRequest = new StringRequest(Request.Method.POST, urlPlaceOrder, 
        new Response.Listener<String>() { 
         @Override 
         public void onResponse(String response) { 
          Toast.makeText(OrderDetails.this, "Response: " + response, Toast.LENGTH_SHORT).show(); 

          JSONObject jsonObjectResponse; 

          try { 
           jsonObjectResponse = new JSONObject(response); 

           boolean status = jsonObjectResponse.getBoolean("success"); 

           if (status) { 

          Toast.makeText(OrderDetails.this, "Order placed successfully!", Toast.LENGTH_SHORT).show(); 

            Intent placeOrderIntent = new Intent(OrderDetails.this,Home.class); 
            startActivity(placeOrderIntent); 

           } else { 
            Toast.makeText(OrderDetails.this, "Nothing!", Toast.LENGTH_SHORT).show(); 
           } 
          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
         } 
        }, 
        new Response.ErrorListener() { 
         @Override 
         public void onErrorResponse(VolleyError error) { 

          Toast.makeText(OrderDetails.this, error.getMessage(), 
            Toast.LENGTH_SHORT).show(); 
         } 
        }) { 
       @Override 
       protected Map<String, String> getParams() throws AuthFailureError { 

        Map<String, String> params = new HashMap<String, String>(); 

        params.put("user_id", getUserId() + ""); 
        params.put("distributor_id", 1 + ""); 

        for (int i = 0; i < productArrayList.size(); i++) { 
         params.put("items", productArrayList.get(i). 
        } 

        params.put("total", totalPriceSum + ""); 
        params.put("mechanic_id", 2 + ""); 

        return params; 
       } 
      }; 

      Volley.newRequestQueue(OrderDetails.this).add(stringRequest); 

     } 
    }); 
} 
+0

Peut-être que vous pouvez l'envoyer en tant que chaîne? Hope [this] (https://stackoverflow.com/questions/42543111/how-to-send-jsonarray-to-server-in-android-using-stringentity) lien vous aide. –

+0

Ça ne marche pas ... –

+0

Pouvez-vous coller si vous obtenez une erreur? –

Répondre

0
JSONArray jsonArray = new JSONArray(productArrayList); 
String jsonArrayString = jsonArray.toString(); 
params.put("items", jsonArrayString + ""); 

essayer convertir liste du tableau JSONArray que JSONArray à cordes et le mettre

+0

Maintenant, jsonArrayString renvoie null? –

+0

Est-ce que productArrayList est nul? –

+0

Non, il contient des objets de données –

0

Vous pouvez convertir la liste en Json en utilisant gson. Essayez comme ci-dessous:

@Override 
protected Map<String, String> getParams() throws AuthFailureError { 

    Map<String, String> params = new HashMap<String, String>(); 
    params.put("user_id", getUserId() + ""); 
    params.put("distributor_id", 1 + ""); 

    // Gson library does this for you 
    params.put("items", new Gson().toJson(productArrayList))); 

    params.put("total", totalPriceSum + ""); 
    params.put("mechanic_id", 2 + ""); 

    return params; 
} 
};