I’m trying to create a loginactivity on Android communicating with a wcf server via xml.
Get Requests work without problem, Post however gives a Request Error.
The webtracelog gives the following:
“There was an error checking start element of object of type LoginMobileParameter. The data at the root level is invalid. Line 1, position 1.”
I suspect a problem with the xml but I just cannot nail it down.
Server Part:
Loginparameter:
[DataContract]
public class LoginMobileParameter
{
[DataMember(Order = 1)]
public string Username { get; set; }
[DataMember(Order = 2)]
public string Password { get; set; }
[DataMember(Order = 3)]
public string DeviceID { get; set; }
}
IService:
[OperationContract]
[WebInvoke(
UriTemplate = "Security/LoginMobile",
Method = "POST",
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml
)]
string LoginMobile(LoginMobileParameter parameter);
Service:
public string LoginMobile(string userName, string password, string deviceID)
{
string decryptedPw = ServiceFactory.EncryptionService.DecryptString(password);
bool loginOK = dataService.CheckLogin(userName, decryptedPw);
if (loginOK)
{
Token t = NewToken;
t.Username = userName;
t.DeviceID = deviceID;
string tokenString = dataService.SaveToken(t);
Log("LoginMobile " + deviceID + ": " + userName + ": Issued Token " + tokenString);
return tokenString;
}
Log("Invalid Username for mobile User " + userName + " / " + password);
return string.Empty; // not authorized
}
Client Part:
XmlGenerator:
public class XmlGenerator {
public static String GenerateLoginXml(String user, String password){
String returnstring = "";
returnstring += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
returnstring += "<LoginMobileParameter><Username>";
returnstring += user;
returnstring += "</Username><Password>";
returnstring += password;
returnstring += "</Password><DeviceID>androidclient</DeviceID></LoginMobileParameter>";
return returnstring;
}
}
Login Button:
...
String xml = XmlGenerator.GenerateLoginXml(username, password);
String url = "security/loginmobile";
String result = con.post(xml, url);
Post:
public class HttpConnection {
public String post(String data, String parameter) {
String urlToSendRequest = "http://my.server.com:1234/Service/" + parameter;
String result = "";
HttpPost httpPost = new HttpPost(urlToSendRequest);
try {
StringEntity entity = new StringEntity(parameter, HTTP.UTF_8);
entity.setContentType("application/xml");
httpPost.setHeader("Content-Type", "application/xml;charset=UTF-8");
httpPost.setEntity(entity);
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
result = EntityUtils.toString(responseEntity);
}
}
catch (Exception ex) {
ex.printStackTrace();
}
return result;
I read through tons of threads and tried everything but just couldn’t make it work.
Thanks in Advance
The following has to be added in the XmlGenerator before the tag:
Also the line
uses the url parameters instead of the data.