2016-07-20 14 views
0

私のプロジェクトでは、AndroidからPHPにパラメータを送信しようとしています。Volley RequestQueueを使用してJson配列をPHPに送信する方法

パラメータの送信方法は次のとおりです。 CartRequestのリストにjson配列は含まれていないことに注意してください。なぜなら、私はそれを行う方法がわからないからです。 ArrayListのJason ArrayへのGSON変換が機能しているかどうかはわかりません。 GSON変換が実際に

public void sendData(){ 
     String total_amount = cartTotal.getText().toString(); 
     String user_id = "28"; 
     calender = Calendar.getInstance(); 
     dateformat = new SimpleDateFormat("yyyy-MM-dd"); 
     timeformat = new SimpleDateFormat("HH:mm:ss"); 
     date = dateformat.format(calender.getTime()); 
     time = timeformat.format(calender.getTime()); 

     listProducts = ShoppingCartHelper.getCartList(); 

     /** Gson gson = new GsonBuilder().create(); 
     JsonArray cartitems = gson.toJsonTree(listProducts).getAsJsonArray(); **/ 

/** UPDATE: Changed GSON to for loop**/ 

     JSONObject obj = new JSONObject(); 
     JSONArray cartitems = new JSONArray(); 
     for (int i=0; i < listProducts.size(); i++) { 
      try { 
      obj.put("id", id); 
      obj.put("quantity",quantity); 
      cartitems.put(obj); 
     } catch (JSONException e) { 
      throw new RuntimeException(e); 
     } 
    } 

     CartRequest cartRequest = new CartRequest(total_amount, user_id, date, time); 
     RequestQueue queue = Volley.newRequestQueue(ShoppingCartActivity.this); 
     queue.add(cartRequest); 
    } 

を働いている場合、私は、私は私のcartRequestにJSON配列cartitemsを含めると、それをキューにしたいチェックすることができるように私はPHPに送信する方法を知りたいと思った理由があります。私は任意の助けに感謝します

public class CartRequest extends StringRequest { 
    private static final String REQUEST_URL = "MY_URL"; 
    private Map<String, String> params; 

    public CartRequest(String total_amount, String user_id, String date, String time){ 
     super(Request.Method.POST, REQUEST_URL, null, null); 
     params = new HashMap<>(); 
     params.put("total_amount", total_amount); 
     params.put("user_id", user_id); 
     params.put("date", date); 
     params.put("time", time); 
    } 

    @Override 
    public Map<String, String> getParams() { 
     return params; 
    } 
} 

:私はここに私のCartRequestコードだcartitems

JSON配列からidquantityが必要になります。前もって感謝します!

UPDATE:ループを追加した後にエラーが発生しました。あなたはcartItems JSONArray JSONArray cartItems =新しいJSONArray()にする必要があり

wrong 2nd argument type. Found: 'org.json.JSONARRAY', required: 'java.lang.String' 

答えて

0

。 ループを実行して、listProductsの項目をcart項目に追加します。各項目のjsonobjectsとしてIDと数量を持つ項目があります。

public CartRequest(String total_amount, String user_id, String date, String time, JSONArray cartItems){ 
    super(Request.Method.POST, REQUEST_URL, null, null); 
    params = new HashMap<>(); 
    params.put("total_amount", total_amount); 
    params.put("user_id", user_id); 
    params.put("date", date); 
    params.put("time", time); 
    params.put("cartitems", cartItems); 
} 
+0

を参照してくださいこんにちは、あなたは上記の私の更新をチェックしてくださいすることができます。ループを追加した後、私のCartRequestでエラーが発生します:間違った第2引数型。 Found: 'org.json.JSONARRAY'、必須: 'java.lang.String' –

+0

ハッシュマップの代わりにjsonobjectを使用 –

0

// JsonArray要求

void jsonArrayRequest(){ 

String url = "your_url_here"; 
RequestQueue queue = Volley.newRequestQueue(this); 
JsonArrayRequest req = new JsonArrayRequest(url, 
      new Response.Listener<JSONArray>() { 
       @Override 
       public void onResponse(JSONArray 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(); 
       } 
      }); 
    queue.addRequest(req); 

}

http://www.androidhive.info/2014/09/android-json-parsing-using-volley/ http://www.androidhive.info/2014/05/android-working-with-volley-library-1/

関連する問題