我从WCF web service
调用Android application
时遇到问题。如果我拨打以下网址"http://10.0.2.2:80/WebService/WebServiceImpl.svc"
,我会收到回复"200 OK"
,但当我尝试在服务"http://10.0.2.2:80/WebService/WebServiceImpl.svc/Test"
内调用该功能时,我会收到回复"400 Bad request"
。
有人可以帮忙吗?
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();
}
答案 0 :(得分: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) {
}
亚历克斯,谢谢你的回答!
答案 1 :(得分:1)
首先在浏览器中测试此网址。如果您遇到同样的问题,请在清单中为您的服务启用Web访问。
如果您的手机可以通过ip 10.0.2.2进行二次检查。