Android呼叫WCF服务
我从Android呼叫WCF服务,android,wcf,web-services,Android,Wcf,Web Services,我从Android应用程序调用wcfweb服务时遇到问题。如果我调用以下URL,“http://10.0.2.2:80/WebService/WebServiceImpl.svc“我得到响应“200 OK”,但是当我尝试调用服务中的函数时,”http://10.0.2.2:80/WebService/WebServiceImpl.svc/Test“我收到一个响应“400个错误请求” 有人能帮忙吗 namespace WebService { public class WebService
Android应用程序
调用wcfweb服务时遇到问题。如果我调用以下URL,“http://10.0.2.2:80/WebService/WebServiceImpl.svc“
我得到响应“200 OK”
,但是当我尝试调用服务中的函数时,”http://10.0.2.2:80/WebService/WebServiceImpl.svc/Test“
我收到一个响应“400个错误请求”
有人能帮忙吗
namespace WebService
{
public class WebServiceImpl : IWebServiceImpl
{
#region IRestServiceImpl Members
public string Test()
{
return "Test pass";
}
#endregion
}
}
namespace WebService
{
[ServiceContract]
public interface IWebServiceImpl
{
[OperationContract]
[WebInvoke(
Method = "GET")]
string Test();
}
}
Android活动:
public void onClick(View v)
{
// TODO Auto-generated method stub
HttpClient client = new DefaultHttpClient();
String SERVER_HOST="10.0.2.2";
int SERVER_PORT = 80;
String URL = "http://10.0.2.2:80/WebService/WebServiceImpl.svc/Test";
HttpHost target = new HttpHost(SERVER_HOST, SERVER_PORT, "http");
HttpGet request = new HttpGet(URL);
try
{
HttpResponse response = client.execute(target,request);
HttpEntity entity = response.getEntity();
MessageBox(response.getStatusLine().toString());
}
catch(Exception e)
{
MessageBox("excepton");
MessageBox(e.toString());
}
}
public void MessageBox(String message){
Toast.makeText(this,message,Toast.LENGTH_LONG).show();
}
首先在浏览器中测试这个url。如果您有相同的问题,请在清单中为您的服务启用web访问
辅助检查ip 10.0.2.2是否可从您的手机访问。我使用“SoapObject”解决此问题
代码的一部分:
public static final String NAMESPACE = "http://tempuri.org/";
public static final String URL = "http://10.0.2.2:80/MyFirstPublishedWebService/WebServiceImpl.svc?wsdl";
public static final String SOAP_ACTION = "http://tempuri.org/IWebServiceImpl/Login";
public static final String METHOD_NAME = "Login";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope =
new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope .dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
}
catch (Exception e) {
}
亚历克斯,谢谢你的回答 您的配置在服务器上看起来如何?它是否具有具有正确行为集的webHttpBinding?