[英]POST from android to WCF Service (OData)
Edit: 编辑:
After doing some more experimentation, I discovered that the request will only work if all of the values are quoted in the JSON string. 经过更多的实验后,我发现只有在JSON字符串中引用了所有值的情况下,该请求才有效。 That is to say that this won't work 那就是说这行不通
{"Text":"test","RatingValue":0.0,"LocationID":5}
and this will 这将
{"Text":"test","RatingValue":"0.0","LocationID":"5"}
What I don't understand is why. 我不明白是为什么。 The first string seems to be a valid JSON string. 第一个字符串似乎是有效的JSON字符串。 Is this a quirk with WCF? 这是WCF的怪癖吗?
Original Post 原始帖子
I am trying to post a new item to a collection from android. 我正在尝试将新项目发布到android的集合中。 I keep getting a response code of 400: Bad Request. 我不断收到响应代码400:错误的请求。 I don't understand what I'm doing wrong and I was hoping someone might be able to help me. 我不明白自己在做什么错,我希望有人可以帮助我。 Here is the java code. 这是Java代码。
HttpURLConnection conn = (HttpURLConnection) uri.toURL().openConnection();
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("User-Agent", userAgent);
conn.setChunkedStreamingMode(0);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.connect();
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.write(data.getBytes());
out.flush();
int code = conn.getResponseCode();
String message = conn.getResponseMessage();
conn.disconnect();
The data is a JSON string that looks like the following: 数据是一个JSON字符串,如下所示:
{"Text":"test","RatingValue":3.0,"ReviewID":0,"LocationID":5}
In this case the ReviewID is the primary key. 在这种情况下,ReviewID是主键。
The URL for the request points to the collection of Ratings. 请求的URL指向评级集合。 If i paste the same location into my browser, it successfully queries the collection. 如果我将相同位置粘贴到浏览器中,它将成功查询集合。 It looks something like this: 看起来像这样:
http://localhost/DataService.svc/Ratings
try this : 尝试这个 :
HttpClient hc = new DefaultHttpClient();
HttpPost hp = new HttpPost("http://localhost/DataService.svc/Ratings");
HttpResponse hr;
JSONObject jo1 = new JSONObject();
joobject.put("Text", "test");
joobject.put("RatingValue", "3.0");
joobject.put("ReviewID", ".0");
joobject.put("LocationID", ".5");
StringEntity se = new StringEntity(joobject.toString(),HTTP.UTF_8);
se.setContentType("application/json");
hp.setEntity(se);
hr = hc.execute(hp);
maybe helpful 也许有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.