How to calculate quantity price for JSON and postal data?

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

I made a call to receive and send JSON data from a Restful Web service (from a URL) this Tutorial

So, here I need to add the price * quantity as an example below:

enter image description here

But I don’t know how to send this calculation for JSON data I googled and tried a little Other options .... Can anyone suggest me this kind for Post JSON data.

I followed this one in POST JSON Data p>

But this should be done by OnClick, it should ask to add the amount of input. Offline (db) its possible But for Custom Listview (Async listview-Online) I can't build

, , ... Google ....

+4
6

, , . , , .

- 1: Serializable , , . / .

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

- 2: , , UserProducts ArrayList, gson, link1 link2.

- 3: total = price * quantity listview setOnItemClickListener, ,

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

- 4: arraylist Serializable ,

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

- 5: ,

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

- 6: , ? , jsonarray jsonobject , jsonobject , .

    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();
    }

:

        {
          "products": [
            {
              "productname": "p1",
              "price": "15",
              "quantity": "6",
              "total": 90
            },
            {
              "productname": "p2",
              "price": "25",
              "quantity": "4",
              "total": 100
            }
          ],
          "grandd_total": 190
        }
+2

json- :

{
"date": "2015-07-03",
"item": [
    {
        "itemname": "xyz",
        "quantity": 6,
        "price": 80
    },
    {
        "itemname": "abc",
        "quantity": 6,
        "price": 80
    }
],
"total": "960",
"currency": "Rs."
}
+2

, , BigDecimal, , , .

volley lib. , , , : http://www.androidhive.info/2014/05/android-working-with-volley-library-1/

, GSON json-. , , :

 public class Money{
    String name;
    int quantay;
    BigDecimal rate;
    BigDecimal total;

    /* make seters and getters */
}

json :

money = new Money("name", 100, 0.54, 54);
Gson gson = new Gson();
String json = gson.toJson(money);

. , Money.

+2

. json?

json object : -

    Double Rate = Double.parseDouble(jsonobject.getString("Rate"));//put api parameter
    int Qty= Integer.parseInt(jsonobject.getString("Qty"));
    Double total = Rate * Qty;
   //set total on your textview lable
    textview.settext(""+total);

, , .

+1

​​ php. - :

double a = 1.50;
double b = 2.00;
double c = a * b;

HTTPPost medthod (, , ), - :

public void postData(){  
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.yourdomain.com/post.php");  

        try {
            List nameValuePairs = new ArrayList(1);
            nameValuePairs.add(new BasicNameValuePair("data1", c));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));  

            HttpResponse response = httpclient.execute(httppost);

            InputStream is = response.getEntity().getContent();
            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayBuffer baf = new ByteArrayBuffer(20);

            int current = 0;

            while((current = bis.read()) != -1){
                baf.append((byte)current);
            }  

            text = new String(baf.toByteArray());
            txtvw.setText(text);
        } catch (ClientProtocolException e) {
        } catch (IOException e) {
    }

, :)

+1

Hope this helps you as above and I checked you need to Grand_Totalpost on Bill Webservice. you simply calculate and transfer this data to Bill Square as described above, and

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

Here is one of Grand Total's failures, as shown on the Billing screen.

(Here we are trying to get Grand total Rs.905)

double Grand_total = 0.0;

for(int i=0;i<userProducts.size();i++)
{
Product product = userProducts.get(postiton);
product.total = product.price * product.quantity;
Grand_total +=product.total;
}

Now you can transfer all the necessary data to Billing Webservice, for example

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);
            productJObject.put("total", userProducts.get(i).total);

            productJArray.put(productJObject);
        }
        mainJObject.put("products", productJArray);
        mainJObject.put("Grand_total", Grand_total);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
+1
source

All Articles