2013-04-07 22 views
0

我正在做一個使用Web服務的機器人應用程序。當我傳入一個字符串時,它有效,但是當我嘗試使用Xstream時,它不起作用。我已經打印出Xstream和我自己的xml字符串,它們是相同的。如果沒有XStream的它看起來像這樣和作品Android的http發佈與字符串,但不是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>" + "[email protected]" + "</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("[email protected]"); 
      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); 

我的web服務看起來像這樣

$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"; 

忽略奇找查詢,我正在使用活動記錄,我知道它正常工作...謝謝

+0

我應該澄清......它返回false並且不會崩潰 – ed209 2013-04-07 23:51:15

回答

-1

我想通了......當我打印出我的xml user_name看起來像這個user__name eventhough它很明顯是命名爲user_name。必須有某種類型的字符集衝突,所以我只是將它重命名而沒有下劃線。

相關問題