I’m trying to save some information from a user that is saved on the android phone to a WCF service. I’m getting a 400 error from the phone and when I try to send the same request to the server using fiddler (when testing on my localhost) my visual studio’s pops up a null pointer in my save method. I’m following this example to the tee and its just not working: Tutorial
Here some of my code:
Here’s my user object in the WCF service:
[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; }
}
Operation contract in my IService
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Bare,
ResponseFormat = WebMessageFormat.Xml,
RequestFormat= WebMessageFormat.Json,
UriTemplate = "saveUser")]
Boolean saveUser(User user);
^ this method is throwing a null pointer because the user is null. I have tried changing the WebMessageBodyStyle to WrappedRequest but that doesn’t help.
Heres the android code for sending the POST Request:
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);
^ the response is giving me a 400 error from the server.
Here is the fiddler sending the json to the localhost:
Here’s the picture
I added the content-length but in the picture it says 0. it really is 86.
Any help would be a appreciated.
If you look at how I was sending the byte[] it was really a string and when the JSON Object got to the service the service couldn’t figure out where to put this information string. I feel like a complete noob after finding this.