我正在学习如何使用创建Android应用程序和互联网服务: 的Eclipse IDE ksoap2版本2.6.5具有依赖性 Visual Studio 2010中的Android ksoap2微软web服务 - 无法将参数传递给web服务
我的方法是遵循这个伟大的教程:http://www.youtube.com/watch?v=v9EowBVgwSo&feature=related
该教程适用于w3schools测试web服务。
然后我试图写我自己的web服务,并使用Visual Studio中提供的helloworld webservice来启动我。我推荐我的代码指向新的web服务(发布在我的开发笔记本电脑上),并以调试模式在手机上运行它。
调用HelloWorld()webservice时,与webservice的通信很好。它甚至从simpleMethod()返回一个结果,但它不包含我通过它的参数。例如当调用simpleMethod时,Iam返回了以下字符串:“你好”
我认为这是一个web服务的问题,因为它适用于w3schools测试web服务,但我坚持,并希望得到一些帮助。
我的代码如下:
的Webservice:
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());
}
}
}
当运行上面的代码,我得到我的屏幕上输出如下:
身份:您好 simpl eMethod {SRT =安迪; }
我希望的结果是:
状态:您好安迪
任何想法?
您是否尝试用SOAP客户端(如SOAPuI)调用此服务?什么是SAOP响应? – OguzOzkeroglu
还没有,但在您的建议下现在下载SOAPUI。我确实使用了内置的webservice测试工具,它成功返回“Hello Andy”。但是,这是在开发笔记本电脑上本地执行的,而不是从android模拟器/手机中执行的。 – agrodude
使用SOAPUI,我从webservice获得了成功的结果。 – agrodude