我試圖保存從android手機上保存的用戶的一些信息到WCF服務。我從手機中收到400錯誤,當我嘗試使用fiddler將相同的請求發送到服務器時(在本地主機上測試時),我的visual studio在我的保存方法中彈出一個空指針。我下面這個例子來發球和它只是不工作:TutorialAndroid到WCF使用JSON 400錯誤請求和空指針
這裏我的一些代碼: 下面是WCF服務我的用戶對象:在我IService
[DataContract]
public class User
{
[DataMember(Name= "userid")]
public int UserId { get; set; }
[DataMember(Name = "username")]
public string username { get; set; }
[DataMember(Name = "password")]
public string password { get; set; }
[DataMember(Name = "information")]
public Byte[] information { get; set; }
}
經營合同
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Bare,
ResponseFormat = WebMessageFormat.Xml,
RequestFormat= WebMessageFormat.Json,
UriTemplate = "saveUser")]
Boolean saveUser(User user);
^此方法拋出空指針,因爲用戶爲空。我曾嘗試將WebMessageBodyStyle更改爲WrappedRequest,但這沒有幫助。
繼承人發送POST請求了Android代碼:
JSONStringer user = new JSONStringer()
.object()
.key("user").object()
.key("userid").value("1").key("username").value(appState.getCurrentUser().username)
.key("password").value(appState.getCurrentUser().password)
.key("information").value(str.toString())
.endObject()
.endObject();
StringEntity entity = new StringEntity(user.toString());
HttpPost request = new HttpPost(new URI(url));
request.setHeader(HTTP.CONTENT_TYPE, "application/json");
request.setHeader("Accept", "application/json");
request.setEntity(entity);
HttpResponse response = httpClient.execute(request);
^響應是給我一個來自服務器的400錯誤。
這裏是小提琴手發送JSON本地主機: Here's the picture 我添加的內容長度,但在圖片,它說0.1真的是86 任何幫助,將一個讚賞。
我建議你使用'JSONObject'和'JSONObject.toString()'代替 - 它更清晰和更有用。 – 2012-04-27 09:00:32
您是否嘗試過使用Fiddler檢查請求? – Rajesh 2012-04-27 09:38:17
我使用小提琴手 – slai47 2012-04-27 15:40:56