我正在学习如何使用: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; }
我希望结果是:
状态:你好安迪
有任何想法吗?