如何使用ksoap2传递参数给一个web服务?

11

我正在使用Eclipse IDE开发Android应用程序。 我正在尝试连接到一个.NET Web服务。 我正在使用ksoap2版本2.3。

当我调用没有参数的Web方法时,它可以正常工作。 当我要传递参数给Web方法时,我会收到null(在调试WebService时我发现的),并且在客户端代码中从Web方法得到null。

代码:

package com.examples.hello;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloActivity extends Activity {
    /** Called when the activity is first created. */
 private static final String SOAP_ACTION = "http://Innovation/HRService/stringBs";

 private static final String METHOD_NAME = "stringBs";

 private static final String NAMESPACE = "http://Innovation/HRService/";
 private static final String URL = "http://196.205.5.170/mdl/hrservice.asmx";
 TextView tv;

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

 }

 public void call()
 {
         try {

          SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
          //PropertyInfo PI = new PropertyInfo();

             //request.addProperty("a", "myprop");

             SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
             envelope.setOutputSoapObject(request);

             envelope.dotNet=true;
             envelope.encodingStyle = SoapSerializationEnvelope.XSD;


             HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

             androidHttpTransport.call(SOAP_ACTION, envelope);

             Object result = (Object)envelope.getResponse();


             String results = result.toString();
             tv.setText( ""+results); 
         } catch (Exception e) {
             tv.setText(e.getMessage());
             }
     }


}

为什么我会得到空响应,如何使用ksoap2传递参数到webservice?


也许这个线程可以帮助你 https://dev59.com/0XNA5IYBdhLWcg3wNa6T 我也开始从头使用基于eclipse的ksoap2。 - Forrest
6个回答

9

替换为

    request.addProperty("a", "myprop"); 

尝试使用

    request.addProperty("arg0", "myprop");         

我不是ksoap2的专家,但我非常确定这会将第一个参数的值设置为您的Web服务函数。对我来说完美地工作。


5

我已经在这方面工作了2天,终于找到了解决办法。我提交了完整的代码,希望这能帮到你。它可以传递参数并获取响应。

.net C# 中 WebService 文件内部:

[WebService(Namespace = "http://something/webservice/v1")]

[WebMethod]
public DateTime[] Function(Guid organizationId, Guid categoryId)
    {
        return ...;
    }

在Android代码内部:

private final static String URL = "http://something/WebServices/WebService.asmx";
private final static String NAMESPACE = "http://something/webservice/v1";


public ArrayList<Object> getSoapObject(String METHOD_NAME, String SOAP_ACTION, Map<String, String> parameters){

try {

        ArrayList<Object> sol = new ArrayList<Object>();
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        if(parameters != null){
            for (Entry<String, String> para : parameters.entrySet()) {
                request.addProperty(para.getKey(), para.getValue());
            }
        }

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet=true;
        envelope.setOutputSoapObject(request);

        Log.d("Body", envelope.bodyOut.toString());

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapObject result=(SoapObject)envelope.getResponse();

            for(int i = 0; i < result.getPropertyCount(); i++){
                sol.add(result.getProperty(i));
            }

            return sol;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }       
    }


    public void getMenuEndDate(String orgId, String categoryId){

        Date startDate = null;
        Date endDate = null;

        HashMap<String, String> parameters = new HashMap<String, String>();
        parameters.put("organizationId", orgId);
        parameters.put("categoryId", categoryId);


        ArrayList<Object> sol = getSoapObject("Function", "http://something/webservice/v1/Function", parameters);

        SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");

        try {
            startDate = (Date)dateFormatter.parse(sol.get(0).toString());
            endDate = (Date)dateFormatter.parse(sol.get(1).toString());
        } catch (ParseException e) {
            Log.d(TAG, "Exception i Date-Formatering");
            e.printStackTrace();
        }

    }

需要检查的事项:

  • 参数名称是否与Web服务所期望的完全相同?
  • 检查命名空间是否以尾部“/”结尾。在应用程序中也要保持一致。

5

通过j2me传递参数调用webservice

SoapObject request = new SoapObject("http://www.webserviceX.NET", "GetCitiesByCountry");
String soapAction = "http://www.webserviceX.NET/GetCitiesByCountry";

request.addProperty("CountryName", "india");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = request;
envelope.dotNet = true;

HttpTransport ht = new HttpTransport("http://www.webservicex.net/globalweather.asmx");
ht.debug = true;
//System.err.println( ht.requestDump );

ht.call(soapAction,envelope);
System.out.println("####################: " +envelope.getResponse());
//SoapObject result = (SoapObject)envelope.getResponse();

4

您需要在客户端代码中声明参数类型:

SoapObject request = new SoapObject("http://tempuri.org/", "mymethod"); 
PropertyInfo p = new PropertyInfo();
p.setName("param_name_from_webservice");
p.setValue(true);
p.setType(Boolean.class);
request.addProperty(p);

4

尝试注释掉以下这行代码:

envelope.dotNet=true;

我做了和你一样的事情,当我读到这个属性是一个非常丑陋的hack时,我为了测试目的注释掉了它,我的参数成功传递了。


1
在这里,您编写的代码顺序存在问题,不用担心,请尝试这个方法,它对我有效。
private class ConversionAsyncTask extends AsyncTask<Void,Void,Void> {
    private SoapPrimitive response;
    protected Void doInBackground(Void... params) {

        SoapObject request = new SoapObject(NAMESPACE, METHOD);


        request.addProperty("a","5");
        SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        soapEnvelope.setOutputSoapObject(request);

        soapEnvelope.dotNet = true;
        soapEnvelope.implicitTypes = true;

        try {
            HttpTransportSE aht = new HttpTransportSE(URL);
            aht.call(SOAP_ACTION, soapEnvelope);

            response = (SoapPrimitive) soapEnvelope.getResponse();

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

    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        temperatureTxt.setText("Status: " + response);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接