2012-04-27 87 views
0

我試圖保存從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 任何幫助,將一個讚賞。

+0

我建議你使用'JSONObject'和'JSONObject.toString()'代替 - 它更清晰和更有用。 – 2012-04-27 09:00:32

+0

您是否嘗試過使用Fiddler檢查請求? – Rajesh 2012-04-27 09:38:17

+0

我使用小提琴手 – slai47 2012-04-27 15:40:56

回答

0

如果你看一下我是如何發送的字節[]這是一個真正的字符串,當JSON對象得到服務的服務不知道在哪裏把這個信息字符串。找到這個之後,我覺得自己是一個完整的noob。

0

嘗試從Fiddler執行POST請求,您的要求應該是如下的內容:

POST http://localhost/SampleApp/Service1.svc/saveUser HTTP/1.1 
Content-Type: application/json 
Host: localhost 

{"userid":1,"username":"testuser","password":"testpassword","information":null} 
+0

我將圖片添加到主帖子中。我遵循你所問的內容,然後添加內容長度。它仍然發送了一個400. – slai47 2012-04-27 13:56:02

+0

我添加了端口,因爲我忘了和Visual Studio的仍然把空指針發送到服務的用戶。所以它現在正在接受請求。 – slai47 2012-04-27 13:58:59

+0

你可以發佈你的原始請求如何? – Rajesh 2012-04-27 20:46:42

0

有時候不好的請求是一個錯誤的錯誤。 例如,如果在WCF服務器端有WritePermissionException,您可能會收到此錯誤錯誤的請求。它可能會讓你感到困惑,並讓你懷疑你的JSON字符串或者與你的請求有關的東西,但是確實導致錯誤的東西是無關緊要的。我花了很多時間來調試這個血腥的壞請求。 所以,在我看來:要解決這個錯誤

最好的辦法是:

  • 使診斷在WCF服務的web.config文件配置。

<system.diagnostics> <sources> <source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true"> <listeners> <add name="xml" /> </listeners> </source> <source name="System.ServiceModel.MessageLogging"> <listeners> <add name="xml" /> </listeners> </source> <source name="myUserTraceSource" switchValue="Information, ActivityTracing"> <listeners> <add name="xml" /> </listeners> </source> </sources> <sharedListeners> <add name="xml" type="System.Diagnostics.XmlWriterTraceListener" initializeData="Traces.svclog" /> </sharedListeners> </system.diagnostics>

  • 那麼,如果從WCF一發布,其託管在本地IIS,並在本地調用它

注:調用REST WCF方法您可以使用REST客戶端,例如(高級REST客戶端),這些客戶端可在Chrome/Firefox中作爲擴展使用。