我正在尝试在通过 xml 与 wcf 服务器通信的 Android 上创建登录活动。
获取请求没有问题,但是发布却给出了请求错误。webtracelog 提供以下内容:
“检查 LoginMobileParameter 类型对象的开始元素时出错。根级别的数据无效。第 1 行,位置 1。”
我怀疑xml有问题,但我无法确定。
服务器部分:
登录参数:
[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; }
}
服务:
[OperationContract]
[WebInvoke(
UriTemplate = "Security/LoginMobile",
Method = "POST",
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml
)]
string LoginMobile(LoginMobileParameter parameter);
服务:
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
}
客户端部分:
xml生成器:
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;
}
}
登录按钮:
...
String xml = XmlGenerator.GenerateLoginXml(username, password);
String url = "security/loginmobile";
String result = con.post(xml, url);
邮政:
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;
我通读了大量线程并尝试了所有方法,但无法使其正常工作。提前致谢