初学Android,很多不懂,各位勿取笑!
低级问题,可能看了都笑,但希望各位不吝教点:
错误提示如下
“java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 54311) after 20000ms: isConnected failed: ECONNREFUSED (Connection refused)”
服务代码,很简单:[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{ [WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod(Description = "返回字符串")]
public string GetString()
{
return "return string";
}
[WebMethod(Description = "两值求合")]
public double GetSum(double a, double b)
{
return a + b;
}
}
Android代码String url="http://localhost:54311/Service1.asmx";
String nameSpace = "http://localhost:54311/";
String methodName = "GetString";
请求WebServiceprotected Object CallWebService(){
String SOAP_ACTION = nameSpace + methodName;
//创建SoapObject实例
SoapObject request=new SoapObject(nameSpace,methodName);
//生成调用web service方法的soap请求消息
SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
//设置.net web service
envelope.dotNet=true;
envelope.bodyOut=request;
(new MarshalBase64()).register(envelope);
//发送请求
//envelope.setOutputSoapObject(request);
//请求参数
if(params != null && !params.isEmpty() ){
for(Iterator it=params.entrySet().iterator();it.hasNext();){
Map.Entry e=(Entry) it.next();
request.addProperty(e.getKey().toString(),e.getValue());
}
}
//
HttpTransportSE htse = new HttpTransportSE(url);
SoapObject result=null;
try{
//web service请求
//androidHttpTrandsport.call(SOAP_ACTION, envelope);
htse.call(SOAP_ACTION, envelope);
//得到返回结果
result=(SoapObject) envelope.getResponse();
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
在上面代码htse.call(SOAP_ACTION, envelope);像上文那样的错误望大神多指点androidwebservice
低级问题,可能看了都笑,但希望各位不吝教点:
错误提示如下
“java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 54311) after 20000ms: isConnected failed: ECONNREFUSED (Connection refused)”
服务代码,很简单:[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{ [WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod(Description = "返回字符串")]
public string GetString()
{
return "return string";
}
[WebMethod(Description = "两值求合")]
public double GetSum(double a, double b)
{
return a + b;
}
}
Android代码String url="http://localhost:54311/Service1.asmx";
String nameSpace = "http://localhost:54311/";
String methodName = "GetString";
请求WebServiceprotected Object CallWebService(){
String SOAP_ACTION = nameSpace + methodName;
//创建SoapObject实例
SoapObject request=new SoapObject(nameSpace,methodName);
//生成调用web service方法的soap请求消息
SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
//设置.net web service
envelope.dotNet=true;
envelope.bodyOut=request;
(new MarshalBase64()).register(envelope);
//发送请求
//envelope.setOutputSoapObject(request);
//请求参数
if(params != null && !params.isEmpty() ){
for(Iterator it=params.entrySet().iterator();it.hasNext();){
Map.Entry e=(Entry) it.next();
request.addProperty(e.getKey().toString(),e.getValue());
}
}
//
HttpTransportSE htse = new HttpTransportSE(url);
SoapObject result=null;
try{
//web service请求
//androidHttpTrandsport.call(SOAP_ACTION, envelope);
htse.call(SOAP_ACTION, envelope);
//得到返回结果
result=(SoapObject) envelope.getResponse();
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
在上面代码htse.call(SOAP_ACTION, envelope);像上文那样的错误望大神多指点androidwebservice
把localhost改成你服务器的ip再试试
order and Export里打钩了
order and Export里打钩了
那个 .net 地址加不加?wsdl无所谓