0

I am using volley for this . this is my code for fetching data from sqlite .Help me to send this json data to server....Thanks in advance

 code : List<ConstTempPlaceorederProduct> contactss = db.getAllContactsPlaceorder();
    JSONObject objProduct = new JSONObject();
    JSONArray productData = new JSONArray();
   for (ConstTempPlaceorederProduct cn : contactss) {
        prod_id=cn.getPid();
        prod_ref_placeorder_id=cn.getRef_pid();
        prod_comp_name=cn.getProd_comp_name();
        prod_cat_name=cn.getProd_cat_name();
        prod_brand_name=cn.getProd_brand_name();
        prod_size=cn.getProd_size();
        prod_unit=cn.getProd_unit();
        prod_mrp=cn.getProd_mrp();
        prod_quantity=cn.getProd_quantity();
        prod_name=cn.getProd_name();
        prod_total=cn.getProd_total();

        JSONObject prodData = new JSONObject();
        prodData.put("id", prod_id);
        prodData.put("name", prod_brand_name);
        productData.put(prodData);

    }

    objProduct.put("all product", productData);
    String result = objProduct.toString();
  • this will help for u https://stackoverflow.com/questions/33014210/how-to-post-request-parameters-when-using-jsonarrayrequest-in-volley – PushpikaWan Oct 27 '18 at 07:14

1 Answers1

0

This is simple sketch. Try this

    // Creating the JsonArrayRequest class called arrayreq, passing the required parameters
    //JsonURL is the URL to be fetched from
    JsonArrayRequest arrayreq = new JsonArrayRequest(JsonURL,
            // The second parameter Listener overrides the method onResponse() and passes
            //JSONArray as a parameter
            new Response.Listener<JSONArray>() {

                // Takes the response from the JSON request
                @Override
                public void onResponse(JSONArray response) {
                    try {
                        // Retrieves first JSON object in outer array
                        JSONObject jsonResponseObj = response.getJSONObject(0);

                    }
                    // Try and catch are included to handle any errors due to JSON
                    catch (JSONException e) {
                        // If an error occurs, this prints the error to the log
                        e.printStackTrace();
                    }
                }
            },
            // The final parameter overrides the method onErrorResponse() and passes VolleyError
            //as a parameter
            new Response.ErrorListener() {
                @Override
                // Handles errors that occur due to Volley
                public void onErrorResponse(VolleyError error) {
                    Log.e("Volley", "Error");
                }
            }
    );
    // Adds the JSON array to the request queue
    requestQueue.add(productData);
}

}

PushpikaWan
  • 2,437
  • 3
  • 14
  • 23