2012-08-07 65 views
3

我从Android application拨打电话WCF web service时出现问题。如果我拨打以下URL,"http://10.0.2.2:80/WebService/WebServiceImpl.svc"我收到回复"200 OK",但是当我尝试调用服务"http://10.0.2.2:80/WebService/WebServiceImpl.svc/Test"内的功能时,我收到了回复"400 Bad request"Android拨打WCF服务

有人可以帮忙吗?

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

你的配置如何在服务器上看起来像?它是否有正确的行为集webHttpBinding? – Rajesh 2012-08-07 16:13:30

回答

1

在浏览器首先测试此URL。如果您遇到同样的问题,请在清单中为您的服务启用Web访问。

辅助检查ip 10.0.2.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) { 

} 

亚历克斯,感谢您的回答!