2011-08-19 52 views
2

如何向此服務發送請求?如何通過Android發送肥皂請求?

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Body> 
    <total xmlns="http://tempuri.org/"> 
     <validationRequest> 
     <Name>string</Name> 
     <num1>int</num1> 
     <num2>int</num2> 
     </validationRequest> 
    </total> 
    </soap:Body> 
</soap:Envelope> 

的Android代碼:

private static final String NAMESPACE ="http://tempuri.org/"; 
private static final String SOAP_ACTION ="http://tempuri.org/total"; 
private static final String URL ="http://10.0.2.2:1743/Service1.asmx"; 
private TextView tv; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    tv = (TextView) findViewById(R.id.tv); 
    tv.setText(ws()); 
} 

private String ws() { 
    String result = ""; 
    try { 
     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);  
     System.out.println("ohaissxh"); 
     PropertyInfo quotesProperty = new PropertyInfo(); 

     request.addProperty("Name","Nas"); 
     request.addProperty("num1",6); 
     request.addProperty("num2",5); 
     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     envelope.dotNet = true; 
     envelope.setOutputSoapObject(request); 
     HttpTransportSE ht = new HttpTransportSE(URL); 
     ht.call(SOAP_ACTION, envelope); 
     System.out.println("dfdjsssf"); 

     if(envelope.getResponse()!=null){ 
      //SoapObject response = (SoapObject)envelope.bodyIn; 
      Object response = envelope.getResponse(); 
      result = response.toString(); 
     } 
    } catch (Exception e) { 
     result = e.getMessage(); 
    } 
    return result; 
} 

回答

0

在您的SOAP請求, <validationRequest>是一個複雜的請求類型,你根本無法在request添加屬性,通常是原始類型來完成。

您需要創建一個類extends Vectorimplements KVMSerializable

public class ValidationReq extends Vector<String> implements KvmSerializable { 


     @Override 
     public Object getProperty(int arg0) { 
       return this.get(arg0); 
     } 

     @Override 
     public int getPropertyCount() { 
       return this.size(); 
     } 

     @Override 
     public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) { 
       arg2.name = "string"; 
       arg2.type = PropertyInfo.STRING_CLASS; 
     } 

     @Override 
     public void setProperty(int arg0, Object arg1) { 
       this.add(arg1.toString()); 
     } 

} 

你的類將包含您要發送的參數,你需要的類的對象添加到request

示例代碼片段可在official KSOAP documentation中找到。

相關問題