我想在Android中创建一个方法,通过为它提供一个字符串参数来与WCF RESTful服务进行通信。
但是,每当我发出请求时,我得到的都是“请求错误”错误。我试过通过Fiddler调用该服务,没有给它任何参数,它工作得很好。
我在这做什么,我该如何解决?
谢谢你的时间!
以下是Java方法的代码:
private void addNewSighting() throws Exception
{
String URL = "http://liveandesor.web711.discountasp.net/wcf/SightingServiceRest.svc/json/SaveNewSightingRest";
AlertDialog popup;
SightingWrapper sighting = new SightingWrapper();
String xml = createXML(sighting);
try{
HttpPost request = new HttpPost(URL);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
JSONObject json = new JSONObject();
json.put("sighting", "holi");
StringEntity sen = new StringEntity( "JSON: " + json.toString());
sen.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
request.setEntity(sen);
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(request);
HttpEntity responseEntity = response.getEntity();
char[] buffer = new char[(int)responseEntity.getContentLength()];
InputStream stream = responseEntity.getContent();
InputStreamReader reader = new InputStreamReader(stream);
reader.read(buffer);
stream.close();
String responseString = new String(buffer);
String holi = responseString;
}
catch(Exception e)
{
}
}
这是我要调用的方法的合同,它位于SightingServiceRest服务中,只返回字符串“IT'S ALIVE !!”:
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "SaveNewSightingRest", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
string SaveNewSightingRest(string sighting);
这是WCF项目的webconfig:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="LiveAndesWCF" connectionString="data source=tcp:sql2k802.discountasp.net;Initial Catalog=SQL2008_832326_liveandes;User ID=randomUser;Password=randomPass; MultipleActiveResultSets=true" providerName="System.Data.SqlClient"/>
<add name="LiveAndes" connectionString="data source=tcp:sql2k802.discountasp.net;Initial Catalog=SQL2008_832326_liveandes;User ID=randomUser;Password=randomPass; MultipleActiveResultSets=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="LiveAndes" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="true" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/>
</providers>
</membership>
</system.web>
<appSettings>
<!--Local-->
<!--
<add key="mainPathDeployWCF" value="http://localhost:61642/"/>
<add key="mainMachPathDeployWCF" value="\LiveAndesWCF\bin"/>
-->
<!-- Deploy -->
<add key="mainPathDeployWCF" value="http://liveandesor.web711.discountasp.net/"/>
<add key="mainMachPathDeployWCF" value="e:\web\liveandesor\htdocs"/>
<!--<add key="mainMachPath" value="C:\Users\Rul\Documents\Universidad\2011'1\Taller de Especialidad\svn\Desarrollo\WEB\LiveAndesMVC\LiveAndesMVC"/>-->
</appSettings>
<system.serviceModel>
<protocolMapping>
<add scheme="http" binding="wsHttpBinding" />
</protocolMapping>
<services>
<service name="LiveAndesWCF.SightingService">
<endpoint address="soap" binding="basicHttpBinding" contract="LiveAndesWCF.ISightingService"/>
<endpoint address="json" binding="webHttpBinding" behaviorConfiguration="jsonBehavior" contract="LiveAndesWCF.ISightingService"/>
</service>
<service name="LiveAndesWCF.UserService">
<endpoint address="soap" binding="basicHttpBinding" contract="LiveAndesWCF.IUserService"/>
<endpoint address="json" binding="webHttpBinding" behaviorConfiguration="jsonBehavior" contract="LiveAndesWCF.IUserService"/>
</service>
<service name="LiveAndesWCF.UserServiceRest" behaviorConfiguration="MetadataBehavior">
<endpoint address="json" behaviorConfiguration="WebBehavior" binding="webHttpBinding" contract="LiveAndesWCF.IUserServiceRest"/>
</service>
<service name="LiveAndesWCF.SightingServiceRest" behaviorConfiguration="MetadataBehavior">
<endpoint address="json" behaviorConfiguration="WebBehavior" binding="webHttpBinding" contract="LiveAndesWCF.ISightingServiceRest"/>
</service>
<service name="LiveAndesWCF.TestService">
<endpoint address="soap" binding="basicHttpBinding" contract="LiveAndesWCF.ITestService"/>
<endpoint address="json" binding="webHttpBinding" behaviorConfiguration="jsonBehavior" contract="LiveAndesWCF.ITestService"/>
</service>
</services>
<bindings>
<basicHttpBinding>
<binding maxReceivedMessageSize="99999999" closeTimeout="00:02:00" openTimeout="00:02:00" receiveTimeout="00:10:00" sendTimeout="00:02:00">
<readerQuotas maxArrayLength="76384000" maxStringContentLength="2147483647"/>
</binding>
</basicHttpBinding>
<webHttpBinding>
<binding maxReceivedMessageSize="99999999" closeTimeout="00:02:00" openTimeout="00:02:00" receiveTimeout="00:10:00" sendTimeout="00:02:00">
<readerQuotas maxArrayLength="76384000" maxStringContentLength="2147483647"/>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="jsonBehavior">
<enableWebScript/>
<!--<webHttp/>-->
</behavior>
<behavior name="WebBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
<behavior name="MetadataBehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl="" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
答案 0 :(得分:0)
前缀JSON:
不正确。你很难在Fiddler的测试中使用它。
将该行替换为:
StringEntity sen = new StringEntity(json.toString());