【发布时间】:2010-06-11 12:12:13
【问题描述】:
从 REST 服务器检索数据效果很好,但如果我想发布一个对象,它就不起作用了:
public static void postJSONObject(int store_type, FavoriteItem favorite, String token, String objectName) {
String url = "";
switch(store_type) {
case STORE_PROJECT:
url = URL_STORE_PROJECT_PART1 + token + URL_STORE_PROJECT_PART2;
//data = favorite.getAsJSONObject();
break;
}
HttpClient httpClient = new DefaultHttpClient();
HttpPost postMethod = new HttpPost(url);
try {
HttpEntity entity = new StringEntity("{\"ID\":0,\"Name\":\"Mein Projekt10\"}");
postMethod.setEntity(entity);
HttpResponse response = httpClient.execute(postMethod);
Log.i("JSONStore", "Post request, to URL: " + url);
System.out.println("Status code: " + response.getStatusLine().getStatusCode());
} catch (ClientProtocolException e) {
我总是收到 400 错误代码。有谁知道怎么回事?
我有可用的 C# 代码,但无法转换:
System.Net.WebRequest wr = System.Net.HttpWebRequest.Create("http://localhost:51273/WSUser.svc/pak3omxtEuLrzHSUSbQP/project");
wr.Method = "POST";
string data = "{\"ID\":1,\"Name\":\"Mein Projekt\"}";
byte [] d = UTF8Encoding.UTF8.GetBytes(data);
wr.ContentLength = d.Length;
wr.ContentType = "application/json";
wr.GetRequestStream().Write(d, 0, d.Length);
System.Net.WebResponse wresp = wr.GetResponse();
System.IO.StreamReader sr = new System.IO.StreamReader(wresp.GetResponseStream());
string line = sr.ReadToEnd();
【问题讨论】:
-
我怀疑只有编写 Web 服务的人才能回答。
-
status为400时请求体中是否有内容?也许他们会回复一条错误消息,告诉您他们为什么给您 400。
-
感谢您的提示。生病检查它。我还添加了一些我知道它可以工作的代码,但不幸的是它的 C#
-
[android defaulthttpclient post to service wcf svc][1] [1]: stackoverflow.com/questions/11893180/…
-
[android-defaulthttpclient-post-to-service-wcf-svc][1] [1]: stackoverflow.com/questions/11893180/…