2009-08-12 88 views

回答

0

如果Web服務可用於休息API,我會使用Restlet。我有一些博客文章顯示使用此庫的簡單示例here

0
public class SOAPActivity extends Activity { 
    private final String NAMESPACE = "http://www.webserviceX.NET/"; 
     private final String URL = "http://www.webservicex.net/ConvertWeight.asmx"; 
     private final String SOAP_ACTION = "http://www.webserviceX.NET/ConvertWeight"; 
     private final String METHOD_NAME = "ConvertWeight"; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     SoapObject soapObject=new SoapObject(NAMESPACE, METHOD_NAME); 

     String weight = "700"; 
     String fromUnit = "Kilograms"; 
     String toUnit = "Grams"; 

     PropertyInfo weightProp =new PropertyInfo(); 
     weightProp.setName("Weight"); 
     weightProp.setValue(weight); 
     weightProp.setType(double.class); 
     soapObject.addProperty(weightProp); 

     PropertyInfo fromProp =new PropertyInfo(); 
     fromProp.setName("FromUnit"); 
     fromProp.setValue(fromUnit); 
     fromProp.setType(String.class); 
     soapObject.addProperty(fromProp); 

     PropertyInfo toProp =new PropertyInfo(); 
     toProp.setName("ToUnit"); 
     toProp.setValue(toUnit); 
     toProp.setType(String.class); 
     soapObject.addProperty(toProp); 

     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     envelope.dotNet = true; 
     envelope.setOutputSoapObject(soapObject); 
     HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 

     try { 
      androidHttpTransport.call(SOAP_ACTION, envelope); 
      SoapPrimitive response = (SoapPrimitive)envelope.getResponse(); 
      Log.i("myApp", response.toString()); 

      TextView tv = new TextView(this); 
      tv.setText(weight+" "+fromUnit+" equal "+response.toString()+ " "+toUnit); 
      setContentView(tv); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    } 

這是SOAP Web服務的簡單的代碼,

相關問題