Android的调用WCF服务 [英] Android call WCF service
本文介绍了Android的调用WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个叫 WCF Web服务
从 Android应用程序
的一个问题。如果我叫下面的网址,http://10.0.2.2:80/WebService/WebServiceImpl.svc
我得到的回应200 OK
,但是当我尝试调用服务中的函数http://10.0.2.2:80/WebService/WebServiceImpl.svc/Test
我得到回应400错误的请求
。
有人能帮助?
命名空间的WebService
{
公共类WebServiceImpl:IWebServiceImpl
{
#区域IRestServiceImpl成员
公共字符串测试()
{
返回检验合格;
} #endregion
}
}WebService的命名空间
{
[服务合同]
公共接口IWebServiceImpl
{
[OperationContract的]
[WebInvoke(
方法=GET)]
字符串Test();
}
}
Android的活动:
公共无效的onClick(视图v)
{
// TODO自动生成方法存根
HttpClient的客户端=新DefaultHttpClient();
字符串SERVER_HOST =10.0.2.2;
INT SERVER_PORT = 80;
字符串的URL =http://10.0.2.2:80/WebService/WebServiceImpl.svc/Test;
HttpHost目标=新HttpHost(SERVER_HOST,SERVER_PORT,HTTP);
HTTPGET请求=新HTTPGET(URL);
尝试
{
HTT presponse响应= client.execute(目标,请求);
HttpEntity实体= response.getEntity();
的MessageBox(response.getStatusLine()的toString());
}
赶上(例外五)
{
的MessageBox(excepton);
的MessageBox(e.toString());
}
} 公共无效的MessageBox(字符串消息){
Toast.makeText(这一点,消息,Toast.LENGTH_LONG).show();
}
解决方案
我解决了使用SoapObject
code的一部分:
公共静态最后弦乐NAMESPACE =http://tempuri.org/;
公共静态最终字符串URL =http://10.0.2.2:80/MyFirstPublishedWebService/WebServiceImpl.svc?wsdl;
公共静态最后弦乐SOAP_ACTION =http://tempuri.org/IWebServiceImpl/Login;
公共静态最后弦乐METHOD_NAME =登陆;SoapObject要求=新SoapObject空间(namespace,METHOD_NAME);
SoapSerializationEnvelope信封=
新SoapSerializationEnvelope(SoapEnvelope.VER11);信封.dotNet = TRUE;envelope.setOutputSoapObject(请求);
HttpTransportSE androidHttpTransport =新HttpTransportSE(URL);尝试{
androidHttpTransport.call(SOAP_ACTION,信封);
}
赶上(例外五){}
亚历克斯,感谢您的回答!
I have a problem with calling WCF web service
from Android application
. If I call the following URL, "http://10.0.2.2:80/WebService/WebServiceImpl.svc"
I get the response "200 OK"
, but when I try to call the function within the service "http://10.0.2.2:80/WebService/WebServiceImpl.svc/Test"
I get a response "400 Bad request"
.
Can someone help?
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 Activity:
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();
}
解决方案
I solve that using "SoapObject"
part of code:
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) {
}
Alex, Thanks for answer!
这篇关于Android的调用WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文