2014-01-11 147 views
0

如果我在解釋問題時犯了錯誤,請原諒我。java.lang.IllegalArgumentException:size <= 0使用kso​​ap2庫消耗WCF .SVC SOAP Web服務

我是新來的SOAP技術。我想調用WCF .NET基於SOAP的Web服務的SOAP請求將類似於下面

<soapenv:Envelope 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/"> 
    <soapenv:Header/> 
<soapenv:Body> 
    <tem:IsServiceLive_V> 
    <!--Optional:--> 
    <tem:version>1</tem:version> 
    </tem:IsServiceLive_V> 
    </soapenv:Body> 
    </soapenv:Envelope> 

,我試圖做這樣的

public class MainActivity extends Activity { 

    public static String SOAP_ACTION1="http://tempuri.org/IService1/IsServiceLive_V"; 
    public static String SOAP_ACTION2="http://tempuri.org/IService1/IsServiceLive_V"; 
    public static String METHOD_NAME1="IsServiceLive_V"; 
    public static String METHOD_NAME2="IsServiceLive_V"; 
    public static String NAMESPACE="http://tempuri.org/"; 
    public static String URL="http://64.27.48.117:3340/service/Service1.svc?wsdl"; 


    Button btnFar,btnCel,btnClear; 

     EditText txtFar,txtCel; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 


     btnFar = (Button)findViewById(R.id.btnFar); 

     btnCel = (Button)findViewById(R.id.btnCel); 

     btnClear = (Button)findViewById(R.id.btnClear); 

     txtFar = (EditText)findViewById(R.id.txtFar); 

     txtCel = (EditText)findViewById(R.id.txtCel); 

     btnFar.setOnClickListener(new View.OnClickListener() 

     { 

        @Override 

        public void onClick(View v) 

        { 

         //Initialize soap request + add parameters 

         try 
         { 
          new SoapResult(1).execute(); 
         } 
         catch(NullPointerException e) 
         { 

         } 

        } 

      }); 
} 



public class SoapResult extends AsyncTask<Void, Void, SoapObject> 
{ 
    int flag_status; 
    SoapObject final_result; 

    public SoapResult(int flag) { 
     // TODO Auto-generated constructor stub 
     flag_status=flag; 
    } 

    @Override 
    protected SoapObject doInBackground(Void... arg0) { 
     // TODO Auto-generated method stub 



     if(flag_status==1) 
     { 
      //Initialize soap request + add parameters 

      SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);  



      //Use this to add parameters 

      /* PropertyInfo property=new PropertyInfo(); 
      property.setName("tmp"); 
      property.setValue("GetRestaurantList"); 
      request.addProperty(property);*/ 

      request.addProperty("version", "1"); 


      //Declare the version of the SOAP request 

      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 



      envelope.setOutputSoapObject(request); 

      System.out.println(request.toString()); 
      envelope.dotNet = true; 



      try { 

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 



        //this is the actual part that will call the webservice 

        androidHttpTransport.call(SOAP_ACTION1, envelope); 

        System.out.println("ok"); 

        // Get the SoapResult from the envelope body. 

        SoapObject result = (SoapObject)envelope.bodyIn; 
        System.out.println(result.toString()); 
        //SoapResult soap=(SoapResult)envelope.getResponse(); 



        //Get the first property and change the label text 
       final_result=result; 

      } catch (Exception e) { 

        e.printStackTrace(); 
        System.out.println("hiiii..."); 
      } 




     return final_result; 
    } 

    @Override 
    protected void onPostExecute(SoapObject result) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(result); 


     if(flag_status==1) 
      //txtCel.setText(result.getProperty(0).toString()); 

     if(flag_status==0) 
     { 
      //    txtFar.setText(result.getProperty(0).toString()); 
     } 



    } 

} 

代碼的請求,並在SOAP格式的SOAP響應應該是這個樣子

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
<s:Body> 
    <IsServiceLive_VResponse xmlns="http://tempuri.org/"> 
     <IsServiceLive_VResult>true</IsServiceLive_VResult> 
    </IsServiceLive_VResponse> 
    </s:Body> 
</s:Envelope> 

,但它給我的異常java.lang.IllegalArgumentException異常:大小< = 0

after the call  
androidHttpTransport.call(SOAP_ACTION1, envelope); 

的XML SOAP請求和響應生成使用湯UI工具,並有一次,我工作的罰款

只是沒有得到我要去的地方錯了一點點的幫助將是可觀的..

+0

你可以檢查/結束你的soap_action1 – kingAm

回答

0

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12); 

得到了我一直在尋找的答案。對於WCF Web服務,我們必須爲每種獲取數據的方法創建單獨的類,因爲它無法從您希望得到結果的代碼運行。因此,如果任何人有我面對,請千萬記住要消耗你的Android項目的WCF web服務的Web服務。我給我的下面簡單的例子,每個方法創建一個不同類的問題

public class GetNearBy { 

public static String HOTEL_NAME="hotel_name"; 
public static String NUMBER_OF_BRANCHES="branches"; 
public static String IMAGE1="image1"; 
public static String IMAGE2="image2"; 
public static String IMAGE3="image3"; 
public static String IMAGE4="image4"; 
public static String IMAGE5="image5"; 
public static String IMAGE6="image6"; 
public static String HAS_EVENT="HasEvent"; 
public static String HAS_OFFER="HasOffer"; 
public static String HAS_MENU="HasMenu"; 
public static String HAS_POPULARFOOD="HasPopularFood"; 
public static String ID="Id"; 
public static String IS_OPEN="IsOpen"; 
public static String LAT="LAT"; 
public static String LONG="LONG"; 
public static String LOCATION="Location"; 
public static String STARS="Stars"; 
public static String FACILITY_COUNT="facility_count"; 
private static final String NAMESPACE = "http://tempuri.org/"; 
private static final String METHOD_NAME = "GetNearBy"; 
    private static final String SOAP_ACTION = "http://tempuri.org/IService1/GetNearBy"; 
    private static final String URL = "http://xxxxxx/service/Service1.svc/soap"; 


    public static ArrayList<HashMap<String, String>> connectToSOAPService() 
    { 

    ArrayList<HashMap<String, String>> data=new ArrayList<HashMap<String,String>>(); 
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 



    // creating SOAP envelope 
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

    // this should be true even in not .NET services 
    envelope.dotNet = true; 
    envelope.setOutputSoapObject(request); 

    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 

    String resultValue = ""; 
    try { 
     // calling an action 
     androidHttpTransport.call(SOAP_ACTION, envelope); 

     // receiving response 
     SoapObject response = (SoapObject) envelope.getResponse(); 



     for(int i=0;i<response.getPropertyCount();i++) 
     { 
      HashMap<String, String> result=new HashMap<String, String>(); 
      SoapObject ele1=(SoapObject)response.getProperty(i); 


      String Hotel_name=ele1.getProperty("BranchName").toString(); 
      result.put(HOTEL_NAME, Hotel_name); 

      data.add(result); 
     } 
    } 
    catch(Exception e) 
    { 

    } 





    return data; 

} 

}

,只是把它從那裏你需要從WCF Web服務獲取結果這樣

GetNearBy.connectToSOAPService(); 
2

有同樣的問題。您的服務可能存在一些問題。

,並使用此相反的

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
+0

不能與更改一起工作..它給出了無法序列化的異常1.一個是我在信封中提供的輸入 –

0

嘗試這樣

final String NAMESPACE = con.getResources().getString(
        R.string.NAMESPACE); 
      final String METHOD_NAME = "ItemsByCountry"; 
      final String SOAP_ACTION = con.getResources().getString(
        R.string.SOAP_ACTION) 
        + METHOD_NAME; 
      final String URL = con.getResources().getString(R.string.URL); 

      SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME); 
      Request.addProperty("userId", UserId); 
      Request.addProperty("countryName", countryName); 

      SoapSerializationEnvelope soapEnvelop; 
      soapEnvelop = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
      soapEnvelop.dotNet = true; 
      soapEnvelop.setOutputSoapObject(Request); 

      HttpTransportSE htp = new HttpTransportSE(URL); 

      htp.call(SOAP_ACTION, soapEnvelop); 

      response = (SoapObject) soapEnvelop.getResponse(); 
相關問題