2012-09-08 40 views
0

我遇到了使用Android RESTful使用者從Web服務獲取簡單輸出的問題。以下是我的代碼和我的輸出。但是,我似乎無法獲得從此Web服務返回的值。 http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheitAndroid Web服務使用Rest - SOAP消息中的錯誤

我會非常感激,如果有人能幫助我弄清楚爲什麼它在SOAP消息

"Server was unable to process request. ---> Data at the root level is  
invalid" 

我的代碼編譯說,而且運行良好

public class MainActivity extends Activity { 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
    StrictMode.setThreadPolicy(policy); 

    BufferedReader in = null; 
    try { 
     HttpClient client = new DefaultHttpClient(); 
     HttpPost request = new HttpPost(
       "http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit"); 

     List<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 
     postParameters.add(new BasicNameValuePair("param1", "77")); 
     UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters); 
     request.setEntity(formEntity); 

     HttpResponse response = client.execute(request); 

     in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
     StringBuffer sb = new StringBuffer(""); 
     String line = ""; 
     String NL = System.getProperty("line.separator"); 
     while ((line = in.readLine()) != null) { 
      sb.append(line + NL); 
     } 
     in.close(); 

     String page = sb.toString(); 
     // Log.i(tag, page); 
     System.out.println(page); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     if (in != null) { 
      try { 
       in.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 
} 

我的輸出

09-08 14:25:25.383: I/System.out(1620): <?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><soap:Code> 
<soap:Value>soap:Receiver</soap:Value></soap:Code><soap:Reason><soap:Text 
xml:lang="en">Server was unable to process request. ---&gt; Data at the root level is 
invalid. Line 1, position 1.</soap:Text></soap:Reason><soap:Detail /></soap:Fault> 
</soap:Body></soap:Envelope> 

09-08 14:25:25.863: D/gralloc_goldfish(1620): Emulator without GPU emulation detected. 
09-08 14:30:00.076: I/Choreographer(1620): Skipped 35 frames! The application may be doing too much work on its main thread. 

回答

1

我認爲你應該按照你提供的網站的步驟,從HTTP POST部分。

從我可以理解請求的url應該是http://www.w3schools.com/webservices/tempconvert.asmx/CelsiusToFahrenheit

而且,之後的參數應該是Celsius=77你的情況。

我與捲曲一個簡單的測試:

curl -d "Celsius=50" http://www.w3schools.com/webservices/tempconvert.asmx/CelsiusToFahrenheit 

,我得到的迴應:

<?xml version="1.0" encoding="utf-8"?> 
<string xmlns="http://tempuri.org/">122</string> 

這似乎是確定。

試試這個在您的onCreate()方法:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 

     StrictMode.setThreadPolicy(policy); 


     BufferedReader in = null; 
     try { 
      HttpClient client = new DefaultHttpClient(); 
      HttpPost request = new HttpPost(
        "http://www.w3schools.com/webservices/tempconvert.asmx/CelsiusToFahrenheit"); 

      String celsius = "50"; 
      request.addHeader("Content-Type", "application/x-www-form-urlencoded"); 

      List<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 
      postParameters.add(new BasicNameValuePair("Celsius", celsius)); 
      UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters); 
      request.setEntity(formEntity); 

      HttpResponse response = client.execute(request); 

      in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
      StringBuffer sb = new StringBuffer(""); 
      String line = ""; 
      String NL = System.getProperty("line.separator"); 
      while ((line = in.readLine()) != null) { 
       sb.append(line + NL); 
      } 
      in.close(); 

      String page = sb.toString(); 
      // Log.i(tag, page); 
      System.out.println(page); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      if (in != null) { 
       try { 
        in.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 

哪裏celsius是你要發佈的值。

我的輸出(logcat中)看起來是這樣的:

06-28 07:57:17.506: I/System.out(1847): <?xml version="1.0" encoding="utf-8"?> 
06-28 07:57:17.506: I/System.out(1847): <string xmlns="http://tempuri.org/">122</string>