java.lang.IllegalArgumentException:size< = 0使用kso​​ap2库来使用WCF .SVC SOAP Web服务

时间:2014-01-11 09:12:50

标签: android wcf soap android-asynctask ksoap2

如果我在解释问题时犯了任何错误,请提前谢谢。

我是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:size&lt; = 0

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

XML SOAP请求和响应是使用Soup UI工具生成的,并且工作正常

只是没有得到我错在哪里一点点的帮助会很明显..

3 个答案:

答案 0 :(得分:2)

一旦我遇到同样的问题。您的服务可能存在一些问题。

并使用此

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);

而不是

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

答案 1 :(得分:0)

得到了我想要的答案。对于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 :(得分: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();