【问题标题】:java.lang.IllegalArgumentException: size <= 0 using ksoap2 library for consuming the WCF .SVC SOAP Web servicejava.lang.IllegalArgumentException: size <= 0 using ksoap2 library for consumption the WCF .SVC SOAP Web service
【发布时间】:2014-01-11 09:12:49
【问题描述】:

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

我是 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

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

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

只是没有找到我出错的地方,一点点帮助将是可观的..

【问题讨论】:

  • 你能在soap_action1结束时检查/吗

标签: android wcf soap android-asynctask ksoap2


【解决方案1】:

有一次我遇到了同样的问题。您的服务可能有问题。

然后使用这个

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);

代替

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

【讨论】:

  • 不使用更改.. 它给出了无法序列化的异常 1. 一个是我在信封中提供的输入
【解决方案2】:

得到了我正在寻找的答案。对于 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();

【讨论】:

    【解决方案3】:

    这样试试

    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-29
      • 2014-05-20
      • 1970-01-01
      • 2020-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多