Android 在emulator中使用kSOAP2使用本地主机WCF服务
我已经把头撞在这堵墙上好几天了,所以非常感谢你的帮助 首先是一点背景知识。我是一名经验丰富的WindowsMobile开发人员,即将进入Android系统,因此我是Java、Android甚至WCF的noob。我已经做了很多关于如何使用kSOAP2在Android中使用WCF应用程序的研究,但无论我做了什么,我能想到的是,由于超时,Android中出现了一个套接字错误 首先,我在VisualStudio2008中创建了一个web服务应用程序,它不需要参数,只需使用字符串值进行响应。web服务代码如下所示:Android 在emulator中使用kSOAP2使用本地主机WCF服务,android,wcf,ksoap2,Android,Wcf,Ksoap2,我已经把头撞在这堵墙上好几天了,所以非常感谢你的帮助 首先是一点背景知识。我是一名经验丰富的WindowsMobile开发人员,即将进入Android系统,因此我是Java、Android甚至WCF的noob。我已经做了很多关于如何使用kSOAP2在Android中使用WCF应用程序的研究,但无论我做了什么,我能想到的是,由于超时,Android中出现了一个套接字错误 首先,我在VisualStudio2008中创建了一个web服务应用程序,它不需要参数,只需使用字符串值进行响应。web服务代码
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
namespace Sample
{
[WebService(Namespace = "http://sample.com/")]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string SayHello()
{
return "Hello, Android. From your friend, WCF";
}
}
}
对于此web服务,我没有调整任何其他设置或对web.config文件进行任何修改
当我运行该服务时,它会打开我的浏览器并指向以下url:
http://localhost:61554/Service1.asmx
接下来,我跳入Eclipse并创建了一个简单的Android项目来使用WCF服务。首先,我更改了AndroidManifest.xml文件,并将以下语句添加到其中:
<uses-permission android:name="android.permission.INTERNET" />
给出错误的代码行是:
androidHttpTransport.call("http://192.168.1.72:61554/SayHello", envelope);
在模拟器中运行时,代码到达该行,暂停一两分钟,然后进入catch块并出现超时异常
有没有关于我在这里遗漏了什么的想法?非常感谢您的帮助。我刚刚解决了这个问题。要访问pc上的本地主机,您需要IP地址10.0.2.2。我在这里找到了答案。现在我没有得到套接字超时,但我得到了一个XMLPullParserException,我一直坚持这个异常。好的,我终于解决了我的问题。对于这些技术的新手来说,有几个因素在起作用。希望我的错误能为其他人节省一些时间 我遇到的第一个问题是代码中的参数错误。线路
androidHttpTransport.call("http://192.168.1.72:61554/SayHello", envelope);
不正确,因为它应该列出web服务的命名空间,后跟方法名。所以我把这句话改成:
androidHttpTransport.call("http://sample.com/SayHello", envelope);
在这次改变之后,我仍然有一个例外,但现在我得到了一个不同的例外,这至少是一个道德上的胜利。新的例外规定如下:
expected START_TAG{http://schemas.xmlsoap.org/soap/envelope/} Envelope(position:START_TAG<html>@1:6 in java.io.InputStreamReader@d3el2cc4)
快速的谷歌搜索帮我解决了这个问题。台词
SoapObject response = (SoapObject)envelope.getResponse();
String resultValue = response.getProperty(0).toString();
在我的原始代码中,需要更改为:
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
String resultValue = response.toString();
在那次改变之后,我有了一份工作申请
最终工作代码
Web服务(在Visual Studio 2008中创建,并按照项目属性中的配置在本地IIS Web服务器上运行)
安卓类
package com.sample;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import org.ksoap2.*;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.*;
import org.ksoap2.serialization.SoapPrimitive;
public class Sample extends Activity {
TextView result;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
result = (TextView)findViewById(R.id.textViewResult);
final String SOAP_ACTION = "http://sample.com/SayHello";
final String METHOD_NAME = "SayHello";
final String NAMESPACE = "http://sample.com/";
final String URL = "http://192.168.1.72/Sample/Service1.asmx";
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
String resultValue = response.toString();
result.setText(resultValue);
}
catch (Exception e) {
result.setText(e.getMessage());
}
}
}
问题标题当然询问了
localhost
,但实际代码使用的是完整的IP地址:192.168.1.72
,因此我不确定这是否是答案。我尝试过使用完整的IP地址“localhost”和“10.0.2.2”解决方案,但没有成功。我不知道后一个选项,所以我很感激现在知道了这一点。在androidHttpTransport.call(“,信封)”中仍然出现错误;请帮忙。
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
String resultValue = response.toString();
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
namespace Sample {
[WebService(Namespace = "http://sample.com/")]
public class Service1 : System.Web.Services.WebService {
[WebMethod]
public string SayHello() {
return "Hello, Android. From your friend, WCF";
}
}
}
package com.sample;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import org.ksoap2.*;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.*;
import org.ksoap2.serialization.SoapPrimitive;
public class Sample extends Activity {
TextView result;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
result = (TextView)findViewById(R.id.textViewResult);
final String SOAP_ACTION = "http://sample.com/SayHello";
final String METHOD_NAME = "SayHello";
final String NAMESPACE = "http://sample.com/";
final String URL = "http://192.168.1.72/Sample/Service1.asmx";
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
String resultValue = response.toString();
result.setText(resultValue);
}
catch (Exception e) {
result.setText(e.getMessage());
}
}
}