如何将以下参数发送到json webservice

时间:2014-09-01 09:37:12

标签: android json android-webservice

我正在开发基于webservice的Android应用程序。我有一个问题如何发送创建后跟随strucure并发布到webservice。遵循结构。

{
    "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"
            }
        ]
    }
}

尝试使用以下代码。

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

所以请建议我如何在webservice中发送pet_data。感谢

2 个答案:

答案 0 :(得分:1)

这应该是tric,您可能需要根据服务器的期望设置更多的头信息,但这就是我将如何做到的。

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

修改

哦,这个问题错了,这会产生类似你的结构:

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

答案 1 :(得分: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;

        }

试试这个,希望它有所帮助。据我所知,您正在使用cordova / phonegap for android。对我来说它有用