1

我正在开发一个基于 web 服务的 android 应用程序。我有一个问题如何发送创建以下结构并发布到网络服务。遵循结构。

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

所以请建议我如何在网络服务中发送 pet_data。谢谢

4

2 回答 2

1

这应该可以解决问题,您可能必须根据服务器的期望设置更多的标头信息,但这就是我的做法。

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);
于 2014-09-01T09:47:10.267 回答
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;

        }

试试这个,希望它有帮助。据我所知,您正在使用适用于 android 的 cordova/phonegap 的标签。对我来说它有效

于 2014-09-01T10:20:54.950 回答