3

WCF web service我从拨打电话时遇到问题Android application。如果我调用以下 URL,"http://10.0.2.2:80/WebService/WebServiceImpl.svc"我会收到 response "200 OK",但是当我尝试在服务中调用该函数时,"http://10.0.2.2:80/WebService/WebServiceImpl.svc/Test"我会收到 response "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();
    }
}

安卓活动:

    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();
    }
4

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) {

}

亚历克斯,谢谢你的回答!

于 2012-08-10T15:15:27.637 回答
1

首先在浏览器中测试这个 url。如果您有同样的问题,请在清单中为您的服务启用 Web 访问。

辅助检查 ip 10.0.2.2 是否可以从您的手机访问。

于 2012-08-07T11:58:46.820 回答