0

我正在制作一个使用网络服务的机器人应用程序。当我传入一个字符串时它可以工作,但是当我尝试使用 Xstream 时它不起作用。我已经打印了 Xstream 和我自己的 xml 字符串,它们是相同的。没有 xstream 它看起来像这样并且可以工作

`HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://softeng.cs.uwosh.edu/students/cs342g6/login.php");
        Boolean response = false;
        try
        {

            String test = "<Login>" +
                    "<password>" + "lernrane" + "</password>" +
                    "<user_name>" + "jake@jake.com" + "</user_name>" +
                    "</Login>";

            StringEntity se = new StringEntity(test,
                    HTTP.UTF_8);
            se.setContentType("text/xml");
            httppost.setEntity(se);
            System.out.println("MADE IT TO RESPONSE");
            HttpResponse httpresponse = httpclient.execute(httppost);
            HttpEntity resEntity = httpresponse.getEntity();
            String resp = EntityUtils.toString(resEntity);
            System.out.println(resp);
            response = convertToBool(resp);`

但是当我使用 xstream 时,它看起来像这样并且失败了

HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://softeng.cs.uwosh.edu/students/cs342g6/login.php");
        Boolean response = false;
        try
        {
            LoginObject login = new LoginObject();
            login.setUserName("jake@jake.com");
            login.setPassword("lernrane");
            XStream xstream = new XStream();
            xstream.alias("Login", LoginObject.class);
            String xml = xstream.toXML(login);
            System.out.println(xml);


            StringEntity se = new StringEntity(xml,
                    HTTP.UTF_8);
            se.setContentType("text/xml");
            httppost.setEntity(se);
            System.out.println("MADE IT TO RESPONSE");
            HttpResponse httpresponse = httpclient.execute(httppost);
            HttpEntity resEntity = httpresponse.getEntity();
            String resp = EntityUtils.toString(resEntity);
            System.out.println(resp);
            response = convertToBool(resp);

我的网络服务看起来像这样

 $dom = new domDocument;
 $dom = simplexml_load_file('php://input');
 $xml = simplexml_import_dom($dom);
 $user = Users::find_by_user_name($xml->user_name);

 if($user)
 {
if($user->password == $xml->password)
{
    echo "TRUE";
}
 }
 else 
     echo "FALSE";

忽略看起来很奇怪的查询,我正在使用活动记录,我知道它可以正常工作......谢谢

4

1 回答 1

-1

我想通了......当我打印出我的 xml 用户名时,它看起来像这个用户名,尽管它被明确命名为用户名。一定存在某种类型的字符集冲突,所以我只是将其重命名为不带下划线。

于 2013-04-08T01:38:23.057 回答