Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How calculate Price into Quantity for a JSON data and Post Data?

Tags:

json

android

post

I am creating an Android application, and I am trying to retrieve data from a Restful Web-service through JSON.

I have performed a call with get and post JSON data from a Restful Web service(from a URL) By this Tutorial

So here I need to add price * quantity like the example below:

enter image description here

But I don't know how to Post this calculation for JSON data I googled and tried few Other options.... Can any one suggest me like this kind for Post JSON data..

I followed this to POST JSON Data

But this Should be Done OnClick, it should ask add enter quantity. Offline(db) its possible But for Custom Listview(Async listview-Online) I am Unable to Build

Please let me Know to Use for this kind... I google many options But I am Help less....

like image 318
Don't Be negative Avatar asked Jan 08 '23 01:01

Don't Be negative


1 Answers

As I understood your question I'm trying to provide answer as follow. I hope that you understand Model class concept, which makes life easier.

Step - 1 : First create one model class and make it Serializable to pass model object, because I can see that you have two activities one for products list and second for billing. In this you may add/remove some fields as per your requirements.

public class Product implements Serializable {
    public String productName;
    public int price;
    public int quantity;
    public int total;
}

Step - 2 : Now I'm assuming that you know how to assign data to ArrayList userProducts by using gson library, link1 and link2.

Step - 3 : Next step would be to calculate total = price * quantity in listview setOnItemClickListener like this,

Product product = userProducts.get(postiton);
product.total = product.price * product.quantity;

Step - 4 : Send arraylist with Serializable object from one activity to another,

    Intent intent = new Intent(ProductActivity.this, BillingActivity.class);
    intent.putExtra("user_products", userProducts);
    startActivity(intent);

Step - 5 : Get values in billing activity,

    if (getIntent() != null) {
        userProducts = (ArrayList<Product>) getIntent()
                .getSerializableExtra("user_products");
    }

Step - 6 : Now your question is how to post them ? The thing is that you've to create jsonarray for list of products and jsonobject for some other fields, and then you can send main jsonobject as a string, a very good tutorial.

    try {
        JSONObject mainJObject = new JSONObject();
        JSONArray productJArray = new JSONArray();
        for (int i = 0; i < userProducts.size(); i++) {
            JSONObject productJObject = new JSONObject();
            productJObject.put("productname", userProducts.get(i).productName);
            productJObject.put("price", userProducts.get(i).price);
            productJObject.put("quantity", userProducts.get(i).quantity);
            productJObject.put("total", userProducts.get(i).total);
            productJArray.put(productJObject);
        }
        mainJObject.put("products", productJArray);
        mainJObject.put("grandd_total", grandTotal);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

It should look like this,

        {
          "products": [
            {
              "productname": "p1",
              "price": "15",
              "quantity": "6",
              "total": 90
            },
            {
              "productname": "p2",
              "price": "25",
              "quantity": "4",
              "total": 100
            }
          ],
          "grandd_total": 190
        }
like image 182
Chitrang Avatar answered Jan 15 '23 08:01

Chitrang