Android 如何将以下参数发送到json Web服务
我正在开发一款基于webservice的android应用程序。我有一个问题,如何发送创建以下结构并发布到Web服务。遵循结构Android 如何将以下参数发送到json Web服务,android,json,android-webservice,Android,Json,Android Webservice,我正在开发一款基于webservice的android应用程序。我有一个问题,如何发送创建以下结构并发布到Web服务。遵循结构 { "petData": { "pet_name_string(petname_gender_size)": [ { "pro_id1": "idoftheprocedure", "pro_price1": "priceoftheprocedure"
{
"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_数据。谢谢这应该可以完成任务,您可能需要根据服务器的期望设置更多的头信息,但我会这样做
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);
试试这个,希望有帮助。据我从标签上猜测,您正在使用android的cordova/phonegap。对我来说,你到目前为止所做的一切都奏效了。?请添加您的代码。我已经用我的代码编辑了我的答案。我已经在代码中添加了一些参数,但我不知道如何发送保留数据。我在如何在web服务中发送保留参数的问题上遇到了问题。非常感谢Denie。这就是我需要的。
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;
}