1

I am working on an android app based on webservice. I have one problem how to send create following strucure and post to the webservice. Following the structure.

{
    "petData": {
        "pet_name_string(petname_gender_size)": [
            {
                "pro_id1": "idoftheprocedure",
                "pro_price1": "priceoftheprocedure"
            },
            {
                "pro_id2": "idoftheprocedure",
                "pro_price2": "priceoftheprocedure"
            },
            {
                "pro_id..n": "idoftheprocedure",
                "pro_price..n": "priceoftheprocedure"
            }
        ]
    }
}

Trying with the following code.

String url = Constents.register_vet_url;
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("content-type", "application/json");
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 25000);
HttpConnectionParams.setSoTimeout(httpParams, 25000);
HttpClient httpClient = new DefaultHttpClient(httpParams);

JSONObject jsonObject = new JSONObject();

jsonObject.put(Constents.vet_name, Constents.vetName);
jsonObject.put(Constents.vet_address, Constents.vetAddress);
jsonObject.put(Constents.vet_email,Constents.vetEmailAdress);

jsonObject.put(Constents.vet_phone,Constents.vetPhoneNumber);
jsonObject.put(Constents.vet_password,Constents.vetPassword);
jsonObject.put(Constents.vet_emergency_service,Constents.vetEmailAdress);
jsonObject.put(Constents.is_represented, String.valueOf(isRepresentedPet));

JSONArray jsonArray = new JSONArray();
JSONObject obj = new JSONObject();
obj.put(Constents.vet_booking_email, Constents.vetEmailAdress);
obj.put(Constents.vet_short_summary, Constents.vetShortSummery);
jsonArray.put(obj);
jsonObject.put(Constents.rep_data, jsonArray);

So please suggest me how to send pet_data in webservice. Thanks

2 Answers2

1

This should do the tric, you might have to set some more header information, based on what your server expects, but thats how i would do it.

DefaultHttpClient hc = new DefaultHttpClient();

HttpPost post = new HttpPost("address to post to");

post.setHeader("Content-Type", "application/json")
post.setEntity(new StringEntitiy(json.toString()));

hc.execute(post);

EDIT

oh got that question wrong, this would generate something like your structure:

JSONObject root = new JSONObject();
JSONObject petData = new JSONObject();

root.put("petData", petData);

JSONArray pets = new JSONArray();

for (each pet){
    JSONObject pet = new JSONObject();
    pet.put("pro_id"+i, "idoftheprocedure");
    pet.put("pro_price"+i,"priceoftheprocedure");

    pets.put(pet.toString());
}

petData.put("pet_name_string(petname_gender_size)", pets);
Daniel Bo
  • 2,518
  • 1
  • 18
  • 29
0
function postdata () {
        var accx = abcd; //your data

//create your json object
        var fromData = {
            value: accx.toString(), // stringify
        };


            var fromDatan = JSON.stringify(fromData);
                //alert(fromDatan);

                //POST JSON  DATA

                $.ajax({
                url: "abcd.com",
                headers: {
                    "X-API-KEY": "wdwdwdwdwdwdwdwdwdwdwd",
                    "Content-Type": "application/json"
                },
                type: "PUT", //POST or PUT whatever you like
                data: fromDatan, //your JSON object
                dataType: "JSON",
                success: function(fromData, status, jqXHR) {
                    //alert(JSON.stringify(fromData));
                },

                //in success function fromData = your JSON object without stringify
                error: function(jqXHR, status) {
                    //alert(JSON.stringify(jqXHR));
                }
                });
                return false;

        }

Try this, hope it helps. As far as I can guess from tags you are working with cordova/phonegap for android. For me It worked

ninja.stop
  • 410
  • 1
  • 10
  • 24