0

我正在学习如何使用:Eclipse IDE ksoap2 版本 2.6.5 和 Visual Studio 2010 创建一个 android 应用程序和 web 服务

我的方法是遵循这个很棒的教程:http ://www.youtube.com/watch?v=v9EowBVgwSo&feature=related

本教程适用于 w3schools 测试网络服务。

然后我尝试编写自己的 Web 服务,并使用 Visual Studio 中提供的 helloworld Web 服务来启动我。我修改了我的代码以指向新的 Web 服务(发布在我的开发笔记本电脑上)并在我的手机上以调试模式运行它。

调用 HelloWorld() webservice 时与 webservice 的通信很好。它甚至从 simpleMethod() 返回一个结果,但它不包括我传递给它的参数。例如,调用 simpleMethod 时,Iam 返回以下字符串:“Hello”

我认为这是网络服务的一个问题,因为它适用于 w3schools 测试网络服务,但我被卡住了,希望能得到一些帮助。

我的代码如下:

网络服务:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WMCMobileWebService
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://testuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public string simpleMethod(String srt)
        {
            return "Hello " + srt;
        }
    }
}

Android 应用 MainActivity:

package com.clcltd.soaptest;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class SoapTestActivity extends Activity {
    private static final String SOAP_ACTION = "http://testuri.org/simpleMethod";
    private static final String METHOD_NAME = "simpleMethod";
    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://192.168.2.22/wmcmobilewebservice/Service1.asmx";
    PropertyInfo pi;
    TextView tv, tvc;
    String str;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.textView1);
        tvc = (TextView) findViewById(R.id.textView2);
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        str = "Andy";

        pi = new PropertyInfo();
        pi.setName("srt");
        pi.setValue(str);
        pi.type = PropertyInfo.STRING_CLASS;

        request.addProperty(pi);

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

        HttpTransportSE aht = new HttpTransportSE(URL);
        try{
            aht.call(SOAP_ACTION, soapEnvelope);
            SoapPrimitive resultString = (SoapPrimitive) soapEnvelope.getResponse();
            tv.setText("Status: " + resultString);
            tvc.setText(request.toString());
        }catch (Exception e)
        {
            e.printStackTrace();
            tv.setText("Error" + e.toString());
        }
    }
}

运行上面的代码时,我在屏幕上得到以下输出:

状态:你好 simpleMethod{srt=Andy; }

我希望结果是:

状态:你好安迪

有任何想法吗?

4

2 回答 2

0

要接收字符串,请尝试以下操作:

SoapObject res = (SoapObject)soapEnvelope.getResponse();
tv.setText("Status: " + res.toString());
于 2012-06-16T14:20:38.383 回答
0

我花了一段时间,但最后我找到了另一个教程(http://sarangasl.blogspot.co.uk/2010/09/create-simple-web-service-in-visual.html)详细说明如何使用旧版本 KSOAP 上的 KSOAP2 Web 服务。

本教程包含一个指向包含旧版本 KSOAP 的代码的链接。

作为测试,我将最新的 KSOAP 版本放在这个新项目中并且它停止工作,所以看起来我的问题毕竟与版本有关。

希望在本教程中指出其他人会帮助他们。

于 2012-07-01T13:51:42.650 回答