簡體   English   中英

從android到WCF服務(OData)的POST

[英]POST from android to WCF Service (OData)

編輯:

經過更多的實驗后,我發現只有在JSON字符串中引用了所有值的情況下,該請求才有效。 那就是說這行不通

{"Text":"test","RatingValue":0.0,"LocationID":5}

這將

{"Text":"test","RatingValue":"0.0","LocationID":"5"}

我不明白是為什么。 第一個字符串似乎是有效的JSON字符串。 這是WCF的怪癖嗎?

原始帖子

我正在嘗試將新項目發布到android的集合中。 我不斷收到響應代碼400:錯誤的請求。 我不明白自己在做什么錯,我希望有人可以幫助我。 這是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();

數據是一個JSON字符串,如下所示:

{"Text":"test","RatingValue":3.0,"ReviewID":0,"LocationID":5}

在這種情況下,ReviewID是主鍵。

請求的URL指向評級集合。 如果我將相同位置粘貼到瀏覽器中,它將成功查詢集合。 看起來像這樣:

http://localhost/DataService.svc/Ratings

嘗試這個 :

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

也許有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM