我试图从Android使用Wcf,我的代码
private final static String SERVICE_URI = "http://188.59.2.211:8081/Survey.svc";
String email = "xx@gmail.com";
String password = "123";
public void onResume() {
super.onResume();
Login(email,password);
}
private void Login(String email, String password)
{
try {
URL url = new URL(SERVICE_URI + "/Login/email="+email+"&password="+password);
HttpGet request = new HttpGet(url.toURI());
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpClient.execute(request);
if ( response != null )
{
Log.i( "login", "received " + response.toString());
}
else
{
Log.i( "login", "got a null response" );
}
HttpEntity responseEntity = response.getEntity();
// Read response data into buffer
char[] buffer = new char[(int)responseEntity.getContentLength()];
InputStream stream = responseEntity.getContent();
InputStreamReader reader = new InputStreamReader(stream);
reader.read(buffer);
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
和wcf服务方,
[ServiceContract(Namespace = "http://saas.com")]
public interface ISurvey
{
[OperationContract]
[WebGet(
UriTemplate = "/Login/email={email}&password={password}",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
LoginResult Login(string email, string password);
}
和我的配置,
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="httpBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<services>
<service name="SAASService.Survey">
<endpoint address=""
behaviorConfiguration="httpBehavior"
binding="webHttpBinding"
contract="SAASService.ISurvey" />
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
问题在于,我正在使用以下网址
“http://188.59.2.211:8081/Survey.svc/Login/email=xx@gmail.com&password=123”
并且始终给出“错误请求”错误,statusCode = 400
有什么问题?
答案 0 :(得分:2)
从Android调用类似的Web服务将无效。此外,SOAP是xml,而不是json,因此您的内容类型是错误的。
要在Android中使用网络服务,最好使用ksoap-android库。 您可以查看this tutorial以获取示例代码。
以下是我在最近的一个项目中使用的示例代码。
private static final String NAMESPACE = "http://webService.protocols.users.mydomain.com/";
private static final String URL = "http://1.1.1.1/domain/ws/userServices?wsdl";
private static final String SOAP_ACTION = "";
public boolean authenticate(String username, String password) {
String METHOD_NAME = "authenticate";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("arg0", username);
request.addProperty("arg1", password);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(URL);
httpTransport.debug = true;
try {
httpTransport.call(SOAP_ACTION, envelope);
} catch (Exception e) {
return false;
}
SoapObject result = null;
try {
result = (SoapObject) envelope.getResponse();
} catch (SoapFault soapFault) {
return false;
}
// anything below this line relates to the service return type, may not apply to you
boolean success = Boolean.valueOf(result.getProperty("success").toString());
if (success)
sessionId.set(result.getProperty("sessionId").toString());
else
error_message.set(result.getProperty("errorMessage").toString());
return success;
}